PDA

View Full Version : [SOLVED:] Count Selected Items in Multselect Listbox?



johndavidson
12-26-2014, 10:18 PM
How do I know when an item has been selected/deselected in a multiselect listbox without having to click the userform or another button and check all the items in the list box. I want to add an automatic count to the number of items selected in a listbox, which activates whenever an item in the listbox is selected/deselected. I expect it is something simple.

I thought that this would do the trick ...

Private Sub lstDocuments_Click()
countSelectedDocs 'function that counts selected items
End Sub

But selecting/deselecting an item in the listbox does not even come to this routine, nor clicking anywhere in the listbox area.

Thanks.

gmayor
12-27-2014, 03:34 AM
Use the following instead to add the count to a label - Label1


Private Sub lstDocuments_Change()
Dim iCount As Long
Dim i As Long
iCount = 0
With Me.lstDocuments
For i = 0 To .ListCount - 1
If .Selected(i) Then iCount = iCount + 1
Next i
End With
If iCount = 1 Then
Me.Label1.Caption = iCount & " item selected"
Else
Me.Label1.Caption = iCount & " items selected"
End If
End Sub

johndavidson
12-27-2014, 04:22 AM
Thanks Graham. It was the *_Change thing I didn't know about.

SamT
12-28-2014, 02:46 PM
Nice job, marking thread solved