sparky2205
Well-known Member
- Joined
- Feb 6, 2013
- Messages
- 507
- Office Version
- 365
- 2016
- Platform
- Windows
Hi folks,
I have a ListBox on a spreadsheet that is populated via vba on Workbook Open.
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.
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?
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?