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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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