ListBox not highlighting selection

sparky2205

Well-known Member
Joined
Feb 6, 2013
Messages
507
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi folks,
I have a ListBox on a spreadsheet that is populated via vba on Workbook Open.
VBA Code:
Private Sub Workbook_Open()

Dim N As Long
    For N = 2 To ActiveWorkbook.Sheets.Count - 1
        Sheets("Setup").ListBox1.AddItem ActiveWorkbook.Sheets(N).Name
        If ActiveWorkbook.Sheets(N).Visible = False Then
            With Sheets("Setup").ListBox1
                .Selected(.ListCount - 1) = True
            End With
        End If
    Next N
    Sheets("Setup").ListBox1.Height = Sheets("Setup").ListBox1.ListCount * 15
    
End Sub

The purpose of the ListBox is to allow the user to click a ListBox Item, in this case a worksheet name, and have that worksheet hidden. Click again to unhide.

VBA Code:
Private Sub ListBox1_Change()
Dim x As Long
Dim pw As String
pw = ""

On Error GoTo errhandler

ThisWorkbook.Unprotect pw
With ListBox1
    For x = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(x) = True Then
        Worksheets(ListBox1.List(x)).Visible = False
    Else
        Worksheets(ListBox1.List(x)).Visible = True
    End If
    Next x
End With
ThisWorkbook.Protect Password:=pw
Exit Sub

errhandler:
ThisWorkbook.Protect Password:=pw

End Sub

I opened the workbook on Friday (26 Apr) and while clicking a ListBox Item hid the associated worksheet, it did not highlight the ListBox Item.
I opened the workbook again on Monday (29 Apr) and everything worked fine. i.e. the ListBox populated correctly and when an item was clicked it hid/unhid the associated worksheet and highlighted/unhighlighted the ListBox Item.
I opened the workbook again today (Tuesday 30 Apr) and it behaves as it did last Friday i.e. clicking a ListBox Item hid the associated worksheet but did not highlight the ListBox Item.

Does anyone have an explanation/fix for this intermittent behaviour?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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