Hide Rows based on 2 specific cell values

Nick70

Active Member
Joined
Aug 20, 2013
Messages
304
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 that have value = "pear" in column A and at the same time value "apple" in column C (in same row).

If cell in column A has "pear" (e.g. in cell A4) but value in cell C4 does not have "apple" then the row is NOT hidden.
For row to be hidden cell A4 must have "pear" and cell C4 must have "apple" so both conditions must be met.

How do I do that?

Thanks,
N.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
One possibility...
VBA Code:
Option Explicit
Sub HideFruit()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ *** Change to actual sheet name ***
    Dim rng As Range, r As Range, i As Long
    Set rng = ws.Range("A2:D100")
    For i = 1 To 99
        If rng.Cells(i, 1) = "pear" And rng.Cells(i, 3) = "apple" Then
            If r Is Nothing Then Set r = rng.Cells(i, 1)
            Set r = Union(r, rng.Cells(i, 1))
        End If
    Next i
    r.EntireRow.Hidden = True
End Sub
 
Upvote 0
Happy to help, and thanks for the feedback 👍 😀

Hello Kevin,

One last thing please.

If I wanted to add sets of 2 conditions how would the code change.

So for example if cell in column A has "pear" (e.g. in cell A4) but value in cell C4 does not have "apple" then the row is NOT hidden AND
if cell in column A has "cherry" (e.g. in cell A4) but value in cell C4 does not have "apple" then the row is NOT hidden.
 
Upvote 0
Please ignore my latest email as sent by mistake :)
 
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,677
Members
449,327
Latest member
John4520

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