Clear Listbox Selection

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
I have some code which generates a selection on a listbox, based on the values in a cell. The code is

Code:
Dim n As Long
        Dim strInput As String, strOutput As String
        Dim varZz As Variant, i As Integer
 
strInput = Sheets("Sheet1").Range("AT" & cboUniqueID.ListIndex + 3) ' i.e. cell with SHEAR; LATERAL BENDING; FLEXION
        varZz = Split(strInput, ", ") 'create array of values using ";" as delimiter
        For i = LBound(varZz) To UBound(varZz) 'loop through values
        strOutput = varZz(i)
        For n = 0 To Listbox1.ListCount - 1
            If Listbox1.Column(0, n) = strOutput Then
                Listbox1.Selected(n) = True
                Exit For
                End If
                Next
            Next i

The Problem I am having is that when I then change the UniqueID the previous selections remain. So before running the above code I need to clear the listbox selection on the UniqueId_Change event.

How can I do this?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Thanks VoG, I have tried this, but it doesn't seem to work. I thought I'd just need to place it above that code. Is this wrong?
 
Upvote 0
Difficult to see where it would go without seeing your other code but the code that I posted should unselect everything in the listbox.
 
Upvote 0
Well there isn't too much in this form at the moment. So essentially it's:

Code:
Private Sub UniqueID_Change()
 
If cboUniqueID.Value = "" Then
    img.Visible = True
Else
    img.Visible = False
End If
 
 
    If UniqueID.ListIndex <> -1 Then
        Textbox1.Value = Sheets("Sheet1").Range("B" & UniqueID.ListIndex + 3) & " " & Sheets("Sheet1").Range("C" & UniqueID.ListIndex + 3)
        Textbox2.Value = Format(CDate(Sheets("Sheet1").Range("AN" & UniqueID.ListIndex + 3)), "dd/mm/yyyy")
        Combobox1.Value = Sheets("Sheet1").Range("AR" & UniqueID.ListIndex + 3)
        Combobox2.Value = Sheets("Sheet1").Range("AS" & UniqueID.ListIndex + 3)
 
            Listbox1.ListIndex = -1
        
            Dim n As Long
            Dim strInput As String, strOutput As String
            Dim varZz As Variant, i As Integer
 
            strInput = Sheets("Sheet1").Range("AT" & UniqueID.ListIndex + 3) 
                varZz = Split(strInput, ", ") 'create array of values using ", " as delimiter
            For i = LBound(varZz) To UBound(varZz) 'loop through values
                strOutput = varZz(i)
            For n = 0 To Listbox1.ListCount - 1
                If Listbox1.Column(0, n) = strOutput Then
                    Listbox1.Selected(n) = True
                    Exit For
                End If
                    Next
                Next i
            End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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