View Full Version : [SOLVED:] Was a specific item selected in a multi-select listbox?
clhare
09-01-2016, 06:35 AM
Hi all! I have a multi-select listbox with 5 items in it on my userform. If one specific item is selected, I need to do something. How do I check to see if that item is selected?
I tried adding the following to the click event for the listbox, but it only works if the box is single-select.
Private Sub lstMyListbox_Click()
With lstMyListbox
If .Selected(2) Then
MsgBox "you selected item B"
End If
End With
End Sub
Thanks!
Cheryl
For a Multiselect control you need to look at each index to see if it is Selected.
This is just example code, It doesn't actually fit your requirements.
Private Sub LBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim i
Dim j
Dim SelectionList(lbox1.ListCount) 'an array of selected Items
Dim Selections As String 'A comma separated String of Selected Items
For i = -1 To lbox1.ListCount - 1
'List indexes start at zero, Counts start at 1, Listindex -1 means no item selected.
If lbox1(i).Selected Then
SelectionList(j) = lbox1(i)
j = j + 1
If i = -1 Then
Selections = "False"
Else
Selections = Selections & ", " & lbox1(i)
End If
End If
Next i
End Sub
gmaxey
09-01-2016, 03:09 PM
Cheryl,
The click event isn't triggered with a multi-select listbox (Since list items are indexed from 0 you may need to change (2) to (1):
Private Sub lstMyListbox_Change()
If lstMyListbox.Selected(2) Then Msgbox "You selected item B"
End Sub
clhare
09-02-2016, 06:58 AM
Thank you! That works perfectly!
Cheryl
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.