Trying to create hide or unhide sheets if selected in a ListBox
. ListBox.Selected(c)
is saying couldn't get selected property. It's having the run-time error and if I end instead of debugging, it is hiding or unhiding some sheets.
I tried use ListIndex
or ListValue
instead of ListCount
but it's not going anywhere!
Private Sub CommandButton1_Click()
Dim str As String
Dim Status As String
With ListBox1
Dim C As Long
For C = 1 To ListBox1.ListCount
str = ListBox1.Column(1, ListBox1.ListIndex)
If ListBox1.Selected(C) = True And str = "Visible" Then
Sheets(C).Visible = False
ElseIf ListBox1.Selected(C) = False And str = "Visible" Then
Sheets(C).Visible = True
End If
If ListBox1.Selected(C) = True And str = "Invisible" Then
Sheets(C).Visible = True
ElseIf ListBox1.Selected(C) = False And str = "Invisible" Then
Sheets(C).Visible = False
End If
Next C
Unload Me
End With
End Sub
When the ListBox items are selected, on hitting commandbutton it will change the sheets visibility status from hide to unhide or vice versa. ListBox
Listbox count starts from zero so when you select for example first item on list it never be reached, but for Sheets
starts from 1, so you need always add 1 to ListBox index to reach it.
EDIT:
Private Sub CommandButton1_Click()
Dim c As Long
dim intListCount as Long
intListCount = Me.ListBox1.ListCount - 1
For c = 0 To intListCount
ThisWorkbook.Sheets(c + 1).Visible = IIf(Me.ListBox1.Selected(c) And Me.ListBox1.List(c, 1) = "Visible", False, True)
Next c
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
i = 0
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem
Me.ListBox1.List(i, 0) = ws.Name
Me.ListBox1.List(i, 1) = IIf(Not ws.Visible, "Hidden", "Visible")
i = i + 1
Next ws
End Sub
Please note that error can prompt when you trying to hide all available sheets in workbook.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.