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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,714
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,714
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,604
Messages
5,625,755
Members
416,133
Latest member
ToseSenpai

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