Link to home
Start Free TrialLog in
Avatar of Bill Warren
Bill WarrenFlag for United States of America

asked on

Hiding groups of Rows in multiple Worksheets in Excel

I Have this Macro to hide a group of Rows in Excel, What would be the way to apply it to multiple Worksheets?

Sub HideRows()
    Dim cell As Range
    For Each cell In Range("J7:J1452")
        If Not IsEmpty(cell) Then
            If cell.Value = 0 Then
                cell.EntireRow.Hidden = True
            End If
        End If
    Next
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also if you want to add a filter lets say you don't want to apply this in sheet1 and sheet2 then you have to use something like this..

Sub HideRows()
    Dim ws As Worksheet
    Dim cell As Range
    For Each ws In ActiveWorkbook.Worksheets
        If UCase(ws.Name) <> "SHEET1" And UCase(ws.Name) <> "SHEET2" Then
            For Each cell In ws.Range("J7:J1452")
                If Not IsEmpty(cell) Then
                    If cell.Value = 0 Then
                        cell.EntireRow.Hidden = True
                    End If
                End If
            Next cell
        End If

    Next ws
End Sub

Open in new window


Please note if statement in VB is case sensitive that's why i have converted your ws.name in upper case in VB...

Saurabh...
In my answer above the

Next
End Sub

should also be in the code.
Avatar of Bill Warren

ASKER

Is there a way to specify exactly which worksheets as opposed to all of them?
Sure. See post ID 40749615.
To specify worksheets..Change this line..

If UCase(ws.Name) <> "SHEET1" And UCase(ws.Name) <> "SHEET2" Then

Open in new window


to this..Assuming you want to run for sheet1 and sheet2 only..

If UCase(ws.Name) = "SHEET1" Or UCase(ws.Name) = "SHEET2" Then

Open in new window


Saurabh...
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.