Hiding lots of rows based on cell values

glad_ir

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

I’m new to VBA, as you’ll guess from my question, and am hoping somebody can help please.

I’m trying to hide a large number of rows based on values in 1 column - ie if A1 = 1 or 2 then hide row 1, if A2 = 1 or 2 then hide row 2 and so on down the rows.

The code below works but the thought of replicating it for a couple of hundred rows isn’t filling me with joy!!! Please can somebody put me out of my misery?

If range(“A1”) = 1 Then Rows(“1:1”).EntireRow.Hidden=True
If range(“A1”) = 2 Then Rows(“1:1”).EntireRow.Hidden=True

Any help is very much appreciated. Thank you
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
VBA Code:
Sub HideRows()

Dim Cell As Range, Value as Long, Count As Long

With ThisWorkbook.Sheets("Sheet1")
        Value = 1       
        Count = 0
                For Each Cell In .Range("A1:A" & .Range("A" & Rows.Count).End(xlUp).Row)
                        If Cell.Value >= 1 And Cell.Value <= Value Then
                                Cell.EntireRow.Hidden = True
                                Value = Value + 1
                                Count = Count + 1
                        End If
                Next Cell
        MsgBox Count & " rows hidden"
End With

End Sub
 
Last edited:
Upvote 0
I may have misread your criteria, are you looking for either 1 or 2 to be found in all cells in Column A, or does the value increase to 3,4,5 etc?
 
Upvote 0
Hi Cortexnotion.
Thank you for replying. I'm looking to apply the same rule in each row - i.e. if there is a 1 or 2 in the rows column A cell then I'd like the row to be hidden.
Thank you
 
Upvote 0
Thanks for confirming - in that case try...

VBA Code:
Sub HideRows()

Dim Cell As Range, Count As Long

With ThisWorkbook.Sheets("Sheet1")
        Count = 0
        For Each Cell In .Range("A1:A" & .Range("A" & Rows.Count).End(xlUp).Row)
                If Cell.Value = 1 Or Cell.Value = 2 Then
                        Cell.EntireRow.Hidden = True
                        Count = Count + 1
                End If
        Next Cell
        MsgBox Count & " rows hidden"
End With

End Sub
 
Upvote 0
Solution
That is perfect! Thank you so much. I understand most of what you've done here but need to do some research on As Long.
thanks again.
 
Upvote 0
Maybe this way
VBA Code:
Sub MM1()
Dim r As Long
For r = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Cells(r, 1) = 1 Or Cells(r, 1) = 2 Then Rows(r).EntireRow.Hidden = True
Next r
End Sub
 
Upvote 0
Assuming Column A contains constants (that is, no formulas), you could also use this non-looping macro as well...
VBA Code:
Sub Hide1sAnd2s()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .EntireRow.Hidden = False
    .Value = Evaluate(Replace("IF(@=1,""=1"",IF(@=2,""=2"",If(@="""","""",@)))", "@", .Address))
    .SpecialCells(xlFormulas).EntireRow.Hidden = True
    .Replace "=", "", xlPart, , , , False, False
  End With
End Sub
 
Upvote 0
Sub MM1() Dim r As Long For r = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1 If Cells(r, 1) = 1 Or Cells(r, 1) = 2 Then Rows(r).EntireRow.Hidden = True Next r End Sub
Many thanks Michael - this works great. Totally agree with the quote as well! Thank you
 
Upvote 0
Sub Hide1sAnd2s() With Range("A1", Cells(Rows.Count, "A").End(xlUp)) .EntireRow.Hidden = False .Value = Evaluate(Replace("IF(@=1,""=1"",IF(@=2,""=2"",If(@="""","""",@)))", "@", .Address)) .SpecialCells(xlFormulas).EntireRow.Hidden = True .Replace "=", "", xlPart, , , , False, False End With End Sub
this also works a treat as well! Thank you Rick
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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