unhiding rows based on data in one column

glad_ir

Board Regular
Joined
Nov 22, 2020
Messages
143
Office Version
  1. 2010
Platform
  1. Windows
Hi,

I'm new to VBA and this forum and wondering if I could request some help please. I am using the code below to unhide rows that contain cells in column H containing the text "Class 1" or "Class 2". I'd like to update the code, or find new code, that only unhides rows where column H contains "Class 1" or "Class 2" and the corresponding cell A in the row contains a 1.

e.g.
- if cell H2 contains "Class 1" or "Class 2" and cell A2=1 then I want row 2 to unhide
- if cell A2 is not equal to 1 I don't want row 2 to unhide whatever is in cell H2
- if cell H2 doesn't contain "Class 1" or "Class 2" I don't want row 2 to unhide whatever is in cell A2.

I don't know where to start with this one!

Any help would be much appreciated
Thank you


Sub macro123()

' hide class 4 and 5 entries

Dim xRg As Range
Application.ScreenUpdating = False
For Each xRg In Range("h107: h350")
If xRg.Value = "Class 1" Then
xRg.EntireRow.Hidden = False

Else
xRg.EntireRow.Hidden = False
End If
Next xRg
For Each xRg In Range("h107: h350")
If xRg.Value = "Class 2" Then
xRg.EntireRow.Hidden = False
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi & welcome to MrExcel.
How about
VBA Code:
Sub gladir()
   Dim Cl As Range
   
   For Each Cl In Range("H107:H350")
      If (Cl.Value = "Class 1" Or Cl.Value = "Class 2") And Cl.Offset(, -7).Value = 1 Then
         Cl.EntireRow.Hidden = False
      End If
   Next
End Sub
 
Upvote 0
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Sub gladir()
   Dim Cl As Range
  
   For Each Cl In Range("H107:H350")
      If (Cl.Value = "Class 1" Or Cl.Value = "Class 2") And Cl.Offset(, -7).Value = 1 Then
         Cl.EntireRow.Hidden = False
      End If
   Next
End Sub
Hi Fluff,

Thank you so much - that is perfect! I understand how you've achieved it with the coding as well which is even better.

cheers, Iain
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,318
Members
449,218
Latest member
Excel Master

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