Hide rows unless they contain specific values in specific cells

Nick70

Active Member
Joined
Aug 20, 2013
Messages
306
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a table in a range (A2:D100) with fruit names.

I would like a macro that hides all rows in table range which do not satisfy one of these 3 conditions:

1. Cell in column A has value "pear" and equivalent cell in column C has value "apple" (e.g. cell A3 = "pear" and cell C3 ="apple")
or else
2. Cell in column A has value "kiwi" and equivalent cell in column C has value "cherry" (e.g. cell A4 = "kiwi" and cell C4="cherry")
or else
3. Cell in column A has value "melon" and equivalent cell in column C has value "fig" (e.g. cell A5 = "melon" and cell C5 ="fig")

How do I do that?

Thanks,
N.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi Nick70,

I suggest you use a helper column instead of using a macro.

=OR(AND(A2="Pear",C2="Apple"),AND(A2="Kiwi",C2="Cherry"),AND(A2="Melon",C2="Fig"))

I put it in D2, you can just drag down to D100, then filter the helper column to TRUE. Thanks.

1684508592828.png
 
Upvote 0
Hi @Nick70, thanks for posting on the forum.

If you want the macro, try the following:
VBA Code:
Sub HideRows()
  Dim duo As String
  Dim c As Range
  
  Application.ScreenUpdating = False
  Cells.EntireRow.Hidden = False
  For Each c In Range("A2", Range("A" & Rows.Count).End(3))
    duo = c.Value & "|" & c.Offset(, 2).Value
    If Not (duo = "pear|apple" Or duo = "kiwi|cherry" Or duo = "melon|fig") Then
      c.EntireRow.Hidden = True
    End If
  Next
  Application.ScreenUpdating = False
End Sub

:)
 
Upvote 0
Solution
Works perfectly thanks!
Macro better because table is actually much larger so cannot use helper column.

:) (y)
 
Upvote 0
One last question please.

How can I amend code so that instead of kiwi in

VBA Code:
duo = "kiwi|cherry"

we can have any text including no text (i.e. empty cell)?

I tried
Code:
duo = "*|cherry"

but it does not work.

Thanks,
N.
 
Upvote 0
duo = "*|cherry"

Try this:

VBA Code:
Sub HideRows()
  Dim duo As String
  Dim c As Range
  
  Application.ScreenUpdating = False
  Cells.EntireRow.Hidden = False
  For Each c In Range("A2", Range("A" & Rows.Count).End(3))
    duo = c.Value & "|" & c.Offset(, 2).Value
    If Not (duo Like "pear|apple" Or duo Like "*|cherry" Or duo Like "melon|fig") Then
      c.EntireRow.Hidden = True
    End If
  Next
  Application.ScreenUpdating = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top