VBA code to hide or unhide code on multiple sheets

superskid

Board Regular
Joined
Aug 25, 2006
Messages
160
I am trying to hide or unhide the same rows on multiple sheets. I get an object expected error on this code.

VBA Code:
Sub ToggleRows()
  Dim arr As Variant
  Dim i As Integer
  arr = Array("YYC P&L", "YEG P&L")
  For i = LBound(arr) To UBound(arr)
    arr(i).Rows("16:50").Hidden = Not arr(i).Rows("16:50").Hidden
  Next
End Sub
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,718
You missed the Worksheets object:
VBA Code:
    Worksheets(arr(i)).Rows("16:50").Hidden = Not Worksheets(arr(i)).Rows("16:50").Hidden
 

superskid

Board Regular
Joined
Aug 25, 2006
Messages
160
That worked thanks, I thought I could have multiple ranges of rows, but this seems to give me an error. "Type Mismatch"

VBA Code:
Worksheets(arr(i)).Rows("1:5,9:17").Hidden = Not Worksheets(arr(i)).Rows("1:5,9:17").Hidden
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,718
Recording a macro shows you need to use Range().EntireRow.Hidden:

VBA Code:
Sub ToggleRows2()
    Dim arr As Variant
    Dim i As Integer
    arr = Array("YYC P&L", "YEG P&L")
    For i = LBound(arr) To UBound(arr)
        Worksheets(arr(i)).Range("1:5,9:17").EntireRow.Hidden = Not Worksheets(arr(i)).Range("1:5,9:17").EntireRow.Hidden
    Next
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,876
Messages
5,627,418
Members
416,245
Latest member
Xterminat

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
Top