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

Bill WarrenIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use a code like this which will apply this in all worksheets of your workbook..

Sub HideRows()
    Dim ws As Worksheet
    Dim cell As Range
    For Each ws In ActiveWorkbook.Worksheets

        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

    Next ws
End Sub

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Sub HideRows()
    Dim cell As Range
Dim ws As Worksheet

For Each ws In Worksheets
   Select Case ws.Name
   Case "Sheet1", "Sheet2" ' Change to the sheets where you have rows you want to hide
    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 Select

Open in new window

Next
End Sub
0
Saurabh Singh TeotiaCommented:
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...
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Martin LissOlder than dirtCommented:
In my answer above the

Next
End Sub

should also be in the code.
0
Bill WarrenIT ManagerAuthor Commented:
Is there a way to specify exactly which worksheets as opposed to all of them?
0
Martin LissOlder than dirtCommented:
Sure. See post ID 40749615.
0
Saurabh Singh TeotiaCommented:
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...
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.