Toggle macro to hide and show multiple sheets

jroo

Board Regular
Joined
May 22, 2003
Messages
157
I have two macros that hide and shows multiple sheets. I'd like to condense this to one macro so I could toggle back and forth to hide and show these sheets with a single click of a button. Can someone help me do that? Thanks...

Sub showYears()
Sheets("Year1").Visible = True
Sheets("Year2").Visible = True
Sheets("Year3").Visible = True
End Sub

Sub HideYears()
Sheets("Year1").Visible = False
Sheets("Year2").Visible = False
Sheets("Year3").Visible = False
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Sub hideunhide()
For i = 1 To 3
    With Sheets("Year" & i)
        .Visible = Not .Visible
    End With
Next i
End Sub
 
Upvote 0
Thanks Dave... but I also have plenty of other sheets in this workbook that do not have the same naming convention. For instance, another set of macros I already wrote have the selected sheets below. Is there a way to write a toggle macro using the macros I already have?

Sub HideSomeTabs()
Sheets("Tables").Visible = False
Sheets("Oracle").Visible = False
Sheets("Qry").Visible = False
Sheets("TRP Results").Visible = False
Sheets("GFORM").Visible = False
Sheets("sep freeze base").Visible = False
End Sub

Sub HideSomeTabs()
Sheets("Tables").Visible = True
Sheets("Oracle").Visible = True
Sheets("Qry").Visible = True
Sheets("TRP Results").Visible = True
Sheets("GFORM").Visible = True
Sheets("sep freeze base").Visible = True
End Sub

I noticed when I recorded a macro to hide multiple sheets in a new workbook, it used an array (see below). I'd like to avoid having to this in my workbook. It would take time to write out: Array("Tables", "Oracle", "Qry", "TRP Results", "GFORM", "sep freeze base") for all my macros

Sub Macro1()
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Array("Sheet2", "Sheet3", "Sheet4")).Select
Sheets("Sheet4").Activate
ActiveWindow.SelectedSheets.Visible = False
End Sub
 
Upvote 0
if the sheets are grouped together it might make it easier as you can refer to the index number of the sheet as opposed to the name.
 
Upvote 0
I'll consider grouping the sheets, but then that would mean I'd have to have the sheets always stay in a certain order. How would I write a macro to specifically order those sheets? If there's an easy way to do that let me know.

Otherwise, maybe I'll just try using an array if that's possible. I tried running these two pieces of code separately, and it didn't work

Sheets(Array("Year1", "Year2", "Year3")).Visible = True

Sheets("Year1", "Year2", "Year3").Visible = True

Am I going about this the right way?
 
Upvote 0
Nevermind. I figured out a way to toggle specified sheets...

Sub HideShowYears()
Application.ScreenUpdating = False
Dim sh
For Each sh In Sheets(Array("Year1", "Year2", "Year3"))
sh.Visible = Not sh.Visible
Next
Application.ScreenUpdating = True
End Sub

Your code did help. Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,730
Members
449,185
Latest member
ekrause77

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