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?
 
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
 
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.