Reputation: 91
I want to hide rows 16 & 17 across the following tabs in my workbook:
There has to be a better and more efficient way to write this code:
Sub Macro1()
Sheet6.Rows("16:17").Hidden = True
Sheet7.Rows("16:17").Hidden = True
Sheet8.Rows("16:17").Hidden = True
End Sub
When this code runs, its take longer than I thought it would.
Any help would be appreciated.
Upvotes: 2
Views: 2191
Reputation: 54777
Sub HideRows()
Dim arr As Variant
Dim i As Integer
arr = Array(Sheet6, Sheet7, Sheet8)
For i = LBound(arr) To UBound(arr)
arr(i).Rows("16:17").Hidden = True
Next
End Sub
Sub ShowRows()
Dim arr As Variant
Dim i As Integer
arr = Array(Sheet6, Sheet7, Sheet8)
For i = LBound(arr) To UBound(arr)
arr(i).Rows.Hidden = False
Next
End Sub
Sub ToggleRows()
Dim arr As Variant
Dim i As Integer
arr = Array(Sheet6, Sheet7, Sheet8)
For i = LBound(arr) To UBound(arr)
arr(i).Rows("16:17").Hidden = Not arr(i).Rows("16:17").Hidden
Next
End Sub
Thanks to:
ProfoundlyOblivious for profoundly suggesting and providing the 'Toggle' version.
GMalc for providing the idea of yet another way (not ever seen by me) of using an Array.
Upvotes: 1
Reputation: 1485
There are several ways; one that comes to mind is adding them to a dictionary and using For Each
to loop through it.
Upvotes: 2
Reputation: 2628
Use an array of worksheets...
Dim ws As Worksheet
For Each ws In Worksheets(Array("Sheet6", "Sheet7", "Sheet8"))
ws.Rows("16:17").Hidden = True
Next
Upvotes: 1