Hide rows if certain values selected

Nick70

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

I have a macro which hides some rows depending on which values we have in cells B2 and B3.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False

    If Range("B2").Value = "Yes" And Range("B3").Value = "OK" Then
    
    For x = 20 To 5 Step -1
            If Cells(x, 1) <> "No" And Cells(x, 1) <> "Not OK" Then
                Rows(x).Hidden = True
            Else
                Rows(x).Hidden = False
            End If
        Next x
      
      
        End If
End Sub

This macro works fine and hides all rows which DO NOT have "No" or "Not OK" in Cells(x,1) if in B2 we have "Yes" and in B3 we have "OK".

If I change code so that we have

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False

'Range F10 = London client owner acting as:

    If Range("B2").Value = "Yes" And Range("B3").Value = "OK" Then
    
    For x = 20 To 5 Step -1
            If Cells(x, 1) = "No" And Cells(x, 1) = "Not OK" Then
                Rows(x).Hidden = True
            Else
                Rows(x).Hidden = False
            End If
        Next x
      
      
        End If
End Sub

I would expect macro to hide all rows which HAVE "No" or "Not OK" in Cells(x,1) if in B2 we have "Yes" and in B3 we have "OK".

But macro does not work this time.

If we put condition = instead of <> the macro stops working.

Does anyone know how I need to change code so that it works with equal sign as well (i.e. =)?

Thanks,
N.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You need to change the And to Or. A single cell cannot be both No and Not OK at the same time.
 
Upvote 0
Solution
Good point I had not spotted that.

Thanks!
N.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,264
Members
448,558
Latest member
aivin

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