Code for multi-select list box

S

ShakeandBake

We are in the process of creating a userform and would like to put a
multi-select list box on the form so that the user can select multiple
selections from the list and have them appear on the completed form. I know
I must go into the control box and use the list box but I don't know the VB
code to get this to work. I would appreciate any help anyone can provide.
Thanks so much.
 
J

Jay Freedman

We are in the process of creating a userform and would like to put a
multi-select list box on the form so that the user can select multiple
selections from the list and have them appear on the completed form. I know
I must go into the control box and use the list box but I don't know the VB
code to get this to work. I would appreciate any help anyone can provide.
Thanks so much.

To let the listbox allow multiple selections, select the list box in the form
designer, go to the Properties list, and set the MultiSelect property to the
value fmMultiSelectExtended (this value makes it respond to the Shift and Ctrl
keys the way most list boxes do). Alternatively, you can set this property
somewhere within the Userform_Initialize routine:

Private Sub UserForm_Initialize()
With ListBox1
.List = Array("one", "two", "three", "four")
.MultiSelect = fmMultiSelectExtended
End With
End Sub

The other thing you need to know is how to find out which items in the list are
selected. You can't just say "give me the selected items". You have to loop
through the values of the .Selected property and find out which ones are True:

Private Sub CommandButton1_Click()
Dim itemList As String
Dim idx As Long
For idx = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(idx) Then
itemList = itemList & vbCr & ListBox1.List(idx)
End If
Next
MsgBox itemList
Me.Hide
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top