Avatar of Bill Warren
Bill Warren
Flag 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

Microsoft ExcelMicrosoft ApplicationsVB Script

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Saurabh Singh Teotia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Martin Liss

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Saurabh Singh Teotia

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...
Martin Liss

In my answer above the

Next
End Sub

should also be in the code.
Bill Warren

ASKER
Is there a way to specify exactly which worksheets as opposed to all of them?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Martin Liss

Sure. See post ID 40749615.
Saurabh Singh Teotia

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...
Martin Liss

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.