Quick VBA code to Hide Rows based on Cell Value

Excel2021

New Member
Joined
Mar 26, 2021
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello,

Somebody named ZVI posted the VBA code below in 2020 which works really quickly for 1 sheet. I am wondering if this code can be altered to include multiple sheets? For my purpose just call them "Sheet 1", "Sheet 2", and "Sheet 3". Thanks.

VBA Code:
Sub HideRows1()

Dim a()
Dim rng As Range
Dim i As Long
Dim ws As Worksheet

' Disable screen updating (CF triggering) and events triggering
Application.ScreenUpdating = False
Application.EnableEvents = False

' Main
Set ws = ActiveSheet
With ws
    .UsedRange.EntireRow.Hidden = False
    a() = .Range("K1", .Cells(.Rows.Count, "K").End(xlUp)).Value
    ' Collect rows to be hidden in the rng
    For i = 1 To UBound(a)
        If a(i, 1) = "HIDE" Then
            If rng Is Nothing Then
                Set rng = .Rows(i)
            Else
                Set rng = Union(rng, .Rows(i))
            End If
        End If
    Next
End With

' Hide rows
If Not rng Is Nothing Then
    rng.EntireRow.Hidden = True
End If

' Restore screen uodating and events triggering
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
See if this does what you want (I cannot really test it out without your data).
VBA Code:
Sub HideRows1()

Dim a()
Dim rng As Range
Dim i As Long
Dim wks As Variant
Dim wk As Variant
Dim ws As Worksheet

'Disable screen updating (CF triggering) and events triggering
Application.ScreenUpdating = False
Application.EnableEvents = False

'List worksheets to include below
wks = Array("Sheet1", "Sheet2", "Sheet3")

'Loop through sheets
For Each wk In wks
    Set ws = ActiveWorkbook.Worksheets(wk)

    With ws
        .UsedRange.EntireRow.Hidden = False
        a() = .Range("K1", .Cells(.Rows.Count, "K").End(xlUp)).Value
        ' Collect rows to be hidden in the rng
        For i = 1 To UBound(a)
            If a(i, 1) = "HIDE" Then
                If rng Is Nothing Then
                    Set rng = .Rows(i)
                Else
                    Set rng = Union(rng, .Rows(i))
                End If
            End If
        Next
    End With

'   Hide rows
    If Not rng Is Nothing Then
        rng.EntireRow.Hidden = True
    End If
    
'   Reset range variable
    Set rng = Nothing
    
Next wk

'Restore screen updating and events triggering
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
See if this does what you want (I cannot really test it out without your data).
VBA Code:
Sub HideRows1()

Dim a()
Dim rng As Range
Dim i As Long
Dim wks As Variant
Dim wk As Variant
Dim ws As Worksheet

'Disable screen updating (CF triggering) and events triggering
Application.ScreenUpdating = False
Application.EnableEvents = False

'List worksheets to include below
wks = Array("Sheet1", "Sheet2", "Sheet3")

'Loop through sheets
For Each wk In wks
    Set ws = ActiveWorkbook.Worksheets(wk)

    With ws
        .UsedRange.EntireRow.Hidden = False
        a() = .Range("K1", .Cells(.Rows.Count, "K").End(xlUp)).Value
        ' Collect rows to be hidden in the rng
        For i = 1 To UBound(a)
            If a(i, 1) = "HIDE" Then
                If rng Is Nothing Then
                    Set rng = .Rows(i)
                Else
                    Set rng = Union(rng, .Rows(i))
                End If
            End If
        Next
    End With

'   Hide rows
    If Not rng Is Nothing Then
        rng.EntireRow.Hidden = True
    End If
  
'   Reset range variable
    Set rng = Nothing
  
Next wk

'Restore screen updating and events triggering
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
See if this does what you want (I cannot really test it out without your data).
VBA Code:
Sub HideRows1()

Dim a()
Dim rng As Range
Dim i As Long
Dim wks As Variant
Dim wk As Variant
Dim ws As Worksheet

'Disable screen updating (CF triggering) and events triggering
Application.ScreenUpdating = False
Application.EnableEvents = False

'List worksheets to include below
wks = Array("Sheet1", "Sheet2", "Sheet3")

'Loop through sheets
For Each wk In wks
    Set ws = ActiveWorkbook.Worksheets(wk)

    With ws
        .UsedRange.EntireRow.Hidden = False
        a() = .Range("K1", .Cells(.Rows.Count, "K").End(xlUp)).Value
        ' Collect rows to be hidden in the rng
        For i = 1 To UBound(a)
            If a(i, 1) = "HIDE" Then
                If rng Is Nothing Then
                    Set rng = .Rows(i)
                Else
                    Set rng = Union(rng, .Rows(i))
                End If
            End If
        Next
    End With

'   Hide rows
    If Not rng Is Nothing Then
        rng.EntireRow.Hidden = True
    End If
  
'   Reset range variable
    Set rng = Nothing
  
Next wk

'Restore screen updating and events triggering
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

See if this does what you want (I cannot really test it out without your data).
VBA Code:
Sub HideRows1()

Dim a()
Dim rng As Range
Dim i As Long
Dim wks As Variant
Dim wk As Variant
Dim ws As Worksheet

'Disable screen updating (CF triggering) and events triggering
Application.ScreenUpdating = False
Application.EnableEvents = False

'List worksheets to include below
wks = Array("Sheet1", "Sheet2", "Sheet3")

'Loop through sheets
For Each wk In wks
    Set ws = ActiveWorkbook.Worksheets(wk)

    With ws
        .UsedRange.EntireRow.Hidden = False
        a() = .Range("K1", .Cells(.Rows.Count, "K").End(xlUp)).Value
        ' Collect rows to be hidden in the rng
        For i = 1 To UBound(a)
            If a(i, 1) = "HIDE" Then
                If rng Is Nothing Then
                    Set rng = .Rows(i)
                Else
                    Set rng = Union(rng, .Rows(i))
                End If
            End If
        Next
    End With

'   Hide rows
    If Not rng Is Nothing Then
        rng.EntireRow.Hidden = True
    End If
   
'   Reset range variable
    Set rng = Nothing
   
Next wk

'Restore screen updating and events triggering
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub
Perfect, that worked. Thanks a lot.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,259
Messages
6,177,485
Members
452,782
Latest member
ZCapitao

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