De-selecting a Selected Item in a Single-Select Listbox

Article contributed by Ibby

Single-select ListBoxes (ie: those with the MultiSelect property set to 0 - fmMultiSelectSingle) don't have a straightforward method for de-selecting an item. You need to create this behaviour with code. Just copy this code into the UserForm module and change all instances of "ListBox1" to the name of your ListBox.

Private iClickedItem As Long

Private Sub ListBox1_MouseDown(ByVal Button As Integer, _
        ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    ' Get index of clicked item in ListBox
    iClickedItem = ListBox1.ListIndex

End Sub

Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift _
        As Integer, ByVal X As Single, ByVal Y As Single)

    ' If clicked item is selected, de-select it
    If ListBox1.ListIndex = iClickedItem Then
        ListBox1.ListIndex = -1
    End If

End Sub


Click to view Terms of Use page

Click to view Disclaimer page