Hide rows if certain values selected

Nick70

Board Regular
Joined
Aug 20, 2013
Messages
199
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,662
Office Version
  1. 365
Platform
  1. Windows
You need to change the And to Or. A single cell cannot be both No and Not OK at the same time.
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,662
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,141,862
Messages
5,709,045
Members
421,609
Latest member
misskittens

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
Top