Apply VBA to a range of worksheets

JamesWayne

New Member
Joined
Sep 6, 2017
Messages
15
Hi All

I'm currently using some VBA to hide rows based on value in a cell and what I would like to do is apply that code to a range of worksheets in the workbook, how would I go about this.

Lets say the worksheet names are called Month1, Month2, Month3 etc through to Month12.

This is my code:

Set rng = Range("A5:A90")
For Each cel In rng
If cel.Value = 0 Then
cel.EntireRow.Hidden = True
ElseIf cel.Value = 1 Then
cel.EntireRow.Hidden = False
End If
Next cel

Thank you
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I ran the VBA on one of my sheets, these two lines performed the task;
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Visible = False

But wait, you said a range of sheets and I haven't supplied that.
Code:
Sub Hide_Sheets
Dim a As Long
    For a = 1 To 3
    
    Sheets("Sheet" & a).Select
    ActiveWindow.SelectedSheets.Visible = False
    Next
end sub
That hid 3 sheets.
 
Last edited:
Upvote 0
I ran the VBA on one of my sheets, these two lines performed the task;
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Visible = False

But wait, you said a range of sheets and I haven't supplied that.
Code:
Sub Hide_Sheets
Dim a As Long
    For a = 1 To 3
    
    Sheets("Sheet" & a).Select
    ActiveWindow.SelectedSheets.Visible = False
    Next
end sub
That hid 3 sheets.

Apologies I meant apply the code I've written that hide rows on the sheet to a range of sheets. Not hide the sheet over a range.
 
Upvote 0
Sorry James,
I haven't long come in from a day trip.

How would that work if you deleted "ActiveWindow.SelectedSheets.Visible = False" from my code and copied in your code from comment #1 ?

To me that's selecting sheets and addressing what you require of your code, yes?
 
Upvote 0
Possibly if it is just certain sheets and not all the sheets...

Code:
Sub Test1()

    Dim Rng As Range, Cel As Range, Sh As Worksheet
    Application.ScreenUpdating = False

    For Each Sh In Sheets(Array("Sheet1", "Sheet2", "Sheet4")) ' change sheet names to suit

        Set Rng = Sh.Range("A5:A90")
        For Each Cel In Rng
            If Cel.Value = 0 Then
                Cel.EntireRow.Hidden = True
            ElseIf Cel.Value = 1 Then
                Cel.EntireRow.Hidden = False
            End If
        Next Cel
    Next
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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