Referencing Listbox Items and Sheets by Name

Schadenfreud

New Member
Joined
Jan 22, 2014
Messages
29
Code:
Dim flag As Boolean

Private Sub UserForm_Initialize()
Dim myWorksheet As Worksheet
    
    flag = True
     With lstSheets
        .MultiSelect = fmMultiSelectMulti
        .ListStyle = fmListStyleOption
        For Each myWorksheet In Worksheets
            If myWorksheet.Visible = xlSheetVisible Then
                'add each visible worksheet to the listbox
                .AddItem myWorksheet.Name
            End If
        Next myWorksheet
    End With
    flag = False
End Sub

Private Sub lstSheets_Change()
Dim s As String
Dim i As Integer

    If flag = False Then
    With lstSheets
        If .ListCount < 1 Then
            .Selected(0) = False
            GoTo errHandler
        End If
    End With
    End If
    
    With lstSheets
    On Error GoTo valueCheck
    i = .Selected(.ListIndex) 'is -1 on the first call
    s = '.Selected(.ListIndex).Value
        If .Selected(i) = True Then
            On Error GoTo errHandler
            'error may occur on the following line
            Worksheets(s).Visible = False
        Else:
            Worksheets(s).Visible = True
        End If
    End With
    
Exit Sub
errHandler:
    MsgBox ("There must be at least one visible sheet!")
    lstSheets.Selected(i) = False
    Exit Sub

valueCheck:
    Exit Sub

End Sub

I made a user form that, with the click of an item in the listbox (which is populated with the worksheet names), will show or hide the matching worksheet. I was using indices, but a problem occurs when there are some hidden sheets to begin with, because the listbox is filled with only the visible worksheets (that's part of the functionality required) and so, when there are hidden worksheets the indices of the listbox are totally different. So, to avoid that kind of bugs I thought I'd use the sheet + listbox names to show/hide the worksheets, but that is proving to be a very hard task.

Can you tell me how I can use the listbox item names + worksheet names to make the "mapping" correctly?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try:
Code:
s = .List(.ListIndex)
 
Upvote 0
How about
Code:
With lstSheets
    For I = 0 to .ListCount - 1
        ThisWorkbook.Sheets(.List(I)).Visible = IIf(.Selected(I), xlSheetVisible, xlSheetHidden)
    Next I
End With
 
Upvote 0
This tries to hide all the sheets in the list and throws an error because there must be at least one visible sheet.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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