VBA Dynamic Control over Sheet Visibility

DixiePiper

New Member
Joined
Oct 19, 2015
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I am using a "Workbook_BeforeClose" operation to hide all worksheets except one. This is working fine for now but as I move through the process, I want to be able to change which sheets are visible. Within the application, I have a login in function to control visibility but when the workbook closes, I want to reset the visible sheets. Controlling visibility reduces visual clutter and focuses the user on what needs to happen at the current stage of the project. Here is where I start to get lost . . .

I have an Admin page that holds "behind the curtain" data and I've managed an ActiveX ListBox that holds the sheet names that might need to be visible when the workbook opens. I'd like to be able to select the sheets and have the "Workbook_BeforeClose" macro use that selection to determine which sheets should be visible/very hidden before the workbook shuts down.

Thanks in advance.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this. Assumes your Admin sheet is named "Admin" and the listbox is "ListBox1".
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim lb As MSForms.ListBox, i As Long
    
    Set lb = ThisWorkbook.Worksheets("Admin").OLEObjects("ListBox1").Object
    
    For i = 0 To lb.ListCount - 1
        If lb.Selected(i) Then
            ThisWorkbook.Worksheets(lb.List(i)).Visible = xlSheetHidden  'or xlSheetVeryHidden
        Else
            ThisWorkbook.Worksheets(lb.List(i)).Visible = xlSheetVisible
        End If
    Next

    If Not ThisWorkbook.Saved Then ThisWorkbook.Save
    
End Sub
 
Last edited:
Upvote 0
John,
Thanks for helping get this started. How would I modify the above to hide all sheets EXCEPT for those selected via the listbox?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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