Control Button Toggle

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
289
Office Version
  1. 2016
Platform
  1. Windows
Right now I have 2 control buttons, one is to hide all sheets except for active one and the other is to unhide all sheets. I would like to make that just one button that changes text so it will do both. Not sure how to go about that. My code is below. Any help would be most appreciated. Thank you.

VBA Code:
Sub Hide()
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> ActiveSheet.Name Then ws.Visible = xlSheetHidden
Next ws

End Sub
Sub UnHide()
Dim ws As Worksheet
Set AC = ThisWorkbook.ActiveSheet

  Application.ScreenUpdating = False

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> ActiveSheet.Name Then ws.Visible = xlSheetVisible
Next ws

AC.Select

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,
try

VBA Code:
Sub Hide()
    Dim ws As Worksheet
    Set ac = ThisWorkbook.ActiveSheet
    
    Application.ScreenUpdating = False
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> ActiveSheet.Name Then ws.Visible = _
        IIf(ws.Visible, xlSheetHidden, xlSheetVisible)
    Next ws
    
    With ac.Buttons(1)
            .Caption = IIf(.Caption = "UnHide", "Hide", "UnHide")
    End With
    ac.Select

End Sub

assumes your button is a forms control button

Dave
 
Upvote 0
Hi,
try

VBA Code:
Sub Hide()
    Dim ws As Worksheet
    Set ac = ThisWorkbook.ActiveSheet
  
    Application.ScreenUpdating = False
  
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> ActiveSheet.Name Then ws.Visible = _
        IIf(ws.Visible, xlSheetHidden, xlSheetVisible)
    Next ws
  
    With ac.Buttons(1)
            .Caption = IIf(.Caption = "UnHide", "Hide", "UnHide")
    End With
    ac.Select

End Sub

assumes your button is a forms control button

Dave
Weird, I get error 400. And when I click "hide" all works as it should, my active sheet is the only sheet left, but when I click "unhide" they all unhide and my active sheet hides....the one with the button(s)!!!
 
Upvote 0
curious - works ok for me

try this small change & see if any different

VBA Code:
Sub Hide()
    Dim ws As Worksheet
    Set ac = ThisWorkbook.ActiveSheet
    
    Application.ScreenUpdating = False
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> ac.Name Then ws.Visible = _
        IIf(ws.Visible, xlSheetHidden, xlSheetVisible)
    Next ws
    
    With ac.Buttons(1)
            .Caption = IIf(.Caption = "UnHide", "Hide", "UnHide")
    End With
    
    With ac
        .Visible = True
        .Select
    End With

End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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