VBA Code to look for "H" and "0" to hide rows

Waffles255

New Member
Joined
Mar 30, 2019
Messages
26
Office Version
  1. 2019
Hi Gents i have this VBA Code and it Looks for H to hide rows is there a way i can make it look for "H" and a "0"?

Sub HideRows()
StartRow = 11
EndRow = 2000
ColNum = 2
For i = StartRow To EndRow
If Cells(i, ColNum).Value = "H" Then
Cells(i, ColNum).EntireRow.Hidden = True
Else
Cells(i, ColNum).EntireRow.Hidden = False
End If
Next i
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I'm not sure you meant this?
VBA Code:
Sub HideRows()
StartRow = 11
EndRow = 2000
ColNum = 2
For i = StartRow To EndRow
If Cells(i, ColNum).Value = "H" OR Cells(i, ColNum).Value = 0 Then
Cells(i, ColNum).EntireRow.Hidden = True
Else
Cells(i, ColNum).EntireRow.Hidden = False
End If
Next i
End Sub
 
Upvote 0
Could you consider processing all the rows at once with AutoFilter instead of looping a row at a time?
I would also recommend always declaring your variable types.

VBA Code:
Sub HideRows_v2()
  Dim StartRow As Long, Endrow As Long, ColNum As Long
  
  StartRow = 11
  Endrow = 2000
  ColNum = 2
  With Cells(StartRow - 1, 2).Resize(Endrow - StartRow + 2)
    .Parent.AutoFilterMode = False
    .AutoFilter Field:=1, Criteria1:="<>H", Operator:=xlAnd, Criteria2:="<>0"
  End With
End Sub
 
Upvote 0
You're welcome. Should be somewhat faster too.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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