Schadenfreud
New Member
- Joined
- Jan 22, 2014
- Messages
- 29
Code:
Private Sub UserForm_Initialize()
Dim myWorksheet As Worksheet
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
End Sub
Private Sub lstSheets_Change()
Dim i As Integer
For i = 0 To lstSheets.ListCount - 1
If lstSheets.Selected(i) = True Then
Worksheets(i + 1).Visible = False 'error occurs on this line
Else: Worksheets(i + 1).Visible = True
End If
Next i
'ErrHandler:
'MsgBox ("There must be at least one visible sheet!")
'Exit Sub
End Sub
This form shows and hides sheets with the click of a checkbox, but when I'm left with just 1 visible form and I try to hide it, naturally I get an error. I tried handling it with this code, right after the line where the error might occur:
Code:
On Error GoTo ErrHandler:
So long story short, I want an error message to pop up when someone tries to hide the last visible sheet, the program should skip the '.Visible = False' line and continue on its merry way.