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?
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,133
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Try:
Code:
s = .List(.ListIndex)
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,917
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
 

Schadenfreud

New Member
Joined
Jan 22, 2014
Messages
29
This tries to hide all the sheets in the list and throws an error because there must be at least one visible sheet.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,140
Messages
5,599,964
Members
414,352
Latest member
macquarie_jchan58

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
Top