Show worksheets based on multiple selections from a Userlist

justDave

Board Regular
Joined
Mar 2, 2006
Messages
83
Hi,

I've not used an awful lot of VBA over the past couple of years but I am desperately trying to get back into it.

I have a quick, and possibly easy, question for you all. Is it possible for me to script via VBA for some hidden tabs to unhide based on a user's selection from a Userlist. For example I may have 21 worksheets, one of which is always viewable upon opening the sheet and contains a Userlist which allows multiple selections (which are also the same name as the other 20 tabs, ie sheet 2, sheet 3 etc etc). Based on the user's selection I would then like to be able to script a button to make the selected sheets Unhidden.

Many thanks in advance for any help you may be able to provide.

Dave
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi..

As far as i know.. you can't have a multiselect combo.. you will need to use a Listbox with Multiselect option enabled. I may be wrong.. but i don't see any Multiselect options on the Combobox control.

Try this..

Add a Listbox (ListBox1) and a CommandButton (CommandButton1) to sheet1.

1. Paste this code into your Workbook Open event..
It will Hide all Sheets except Sheet 1.
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
Dim wsObj As Object
  For Each ws In Worksheets
    If ws.Name <> "Sheet1" Then ws.Visible = xlSheetHidden
  Next
  Sheets("Sheet1").ListBox1.Clear


For Each wsObj In Sheets
    If wsObj.Visible = xlSheetHidden Then
        Sheets("Sheet1").ListBox1.AddItem wsObj.Name
    ElseIf wsObj.Visible = xlSheetVeryHidden Then
        ListBox1.AddItem ws.Name & "*"
    End If
Next
End Sub

2. Paste this code into Sheet1

It will make it so the Listbox ONLY displays sheets that are Hidden.
It will Unhide any sheets you select after pressing the Command Button.
You can multiselect.

Code:
Private Sub CommandButton1_Click()
Dim I As Long
Dim J As Long
Dim UnhSh As String
Dim arrItems() As String
    ReDim arrItems(0 To ListBox1.ColumnCount - 1)
    For J = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(J) Then
            For I = 0 To ListBox1.ColumnCount - 1
                arrItems(I) = ListBox1.Column(I, J)
            Next I
            UnhSh = Join(arrItems, ",")
        End If
        If ListBox1.Selected(J) = True Then
        Worksheets(UnhSh).Visible = xlSheetVisible
        End If
    Next J
 Worksheets(UnhSh).Activate
 Sheets("Sheet1").Activate
End Sub




Private Sub Worksheet_Activate()
ListBox1.Clear
Dim ws As Object
For Each ws In Sheets
    If ws.Visible = xlSheetHidden Then
        ListBox1.AddItem ws.Name
    ElseIf ws.Visible = xlSheetVeryHidden Then
        ListBox1.AddItem ws.Name & "*"
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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