Macro to hide rows on multiple sheets

Rusty315

Board Regular
Joined
Sep 13, 2012
Messages
54
Hi everyone

I'm new to VBA (which is to say I have no idea what I'm doing). I'm trying to create a macro that will hide empty rows on various worksheets when the workbook is opened. I have a similar piece of code that I've used successfully in another workbook:

Code:
Private Sub Workbook_Open()

Dim myRng As Range
Dim myCell As Range
With Worksheets("League Table")
Set myRng = .Range("B3:B83")
For Each myCell In myRng.Cells
myCell.EntireRow.Hidden = CBool(myCell.Value = "")
Next myCell
End With

Dim myCell1 As Range
With Worksheets("Team Summaries")
Set myRng = .Range("B14:B34")
For Each myCell In myRng.Cells
myCell.EntireRow.Hidden = CBool(myCell.Value = "0")
Next myCell
End With

End Sub

I need to use this code in a different workbook where I have around 30 pages for individual sales people in our team with all sheets formatted in exactly the same way. I would like to make this code work on all of those sheets but I don't want to have to tinker with this macro every time someone new joins the team so I'm wondering if I can make this macro apply to every sheet in the workbook without having to name each sheet individually in the macro which would mean having to change the macro every time someone joins or leaves.


Any help anyone can give me would be greatly appreciated.

Cheers!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try eg:

Code:
Private Sub Workbook_Open()
    Dim ws As Worksheet
    Dim myRng As Range
    Dim myCell As Range
    For Each ws In ThisWorkbook.Worksheets
        With ws
            Set myRng = .Range("B3:B83")
            For Each myCell In myRng.Cells
                myCell.EntireRow.Hidden = CBool(myCell.Value = "")
            Next myCell
        End With
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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