Clearing ListBox Selection

dhosi439

Board Regular
Joined
May 13, 2009
Messages
62
I'm using the code below to hide or unhide and activate worksheets in my workbook. I would like to add code to deselect the original selection in the listbox, however when I try to add code to several places I receive a Run-Time 9 error, subscript out of range.

Code:
Private Sub lbSheets_Change()

Dim i As Integer, sht As String

    For i = 0 To lbSheets.ListCount - 1
        If lbSheets.Selected(i) = True Then
            sht = lbSheets.List(i)
        End If
        
    Next i
        If Sheets(sht).Visible = True Then
            Sheets(sht).Visible = False
        Exit Sub
        ElseIf Sheets(sht).Visible = False Then
            Sheets(sht).Visible = True
            Sheets(sht).Activate
            End
        End If
    End
End Sub

Code I have tried:

Code:
    For x = 0 To lbSheets.ListCount - 1
    If lbSheets.Selected(x) = True Then
    lbSheets.Selected(x) = False
    End If
    Next
    End

and...

Code:
frmManageSheets.Listbox1.Listindex=-1

I'm sure this has to do with the FOR statement, but I'm not sure what the issue is or how to resolve. Any help would be great.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I'm still having issues...

This is what I currently have:

Code:
Private Sub lbSheets_Change()

Dim i As Integer, sht As String

    For i = 0 To lbSheets.ListCount - 1
        If lbSheets.Selected(i) = True Then
            sht = lbSheets.List(i)
        End If
        
        lbSheets.ListIndex = -1
        
    Next i
        [COLOR="Red"]If Sheets(sht).Visible = True Then[/COLOR]
            Sheets(sht).Visible = False
        Exit Sub
        ElseIf Sheets(sht).Visible = False Then
            Sheets(sht).Visible = True
            'Sheets(sht).Activate
            'End
        Exit Sub
        End If
    End
End Sub

I receive a Run-time error '9': Subscript our of range for the red text above, which is highlighted in the editor.

I have also tried placing the snippet of code below into the first if statement and receive the same error.

Code:
lbSheets.ListIndex = -1

Also this is a change event for a ListBox.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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