Exploiting Multi-Select option in List Box

Unlike other blog, this won't be a long one. To get a quick overview of what list box are and what they can do, follow this link. To get an exhaustive and deep insight of Listbox in Excel-VBA, the provided link won't be effective. In that case, you need to practice and create a form loaded with listbox. Let me not get into all that. Else it will divert my attention in some other direction.

To make things easier, I have created a sample form with a Listbox. The values are populated from Sheet1, range varying from A1 to A8.

The sample Excel sheet values are as shown below.


RowSource property of ListBox shows range from A1 to A8. If you observe carefully, MultiSelect is set to frmMultiSelectSingle. This does not allow us to do multiple selection.




The value of MultiSelect was set to frmMultiSelectMulti ,as shown, to support multi-selection.

Apart from this, I added a command button "OK" in the form. When user clicks "OK", whatever is selected in listbox gets displayed. Following code snippet was updated to perform the activity.

Private Sub CommandButton1_Click()
Dim a As String
For list_count = 1 To ListBox1.ListCount
'ListBox1.ListCount gives a count of items in ListBox
'ListBox1.List(list_count - 1) returns the value for Index list_count-1
    If (ListBox1.Selected(list_count - 1) = True) Then a = a & ListBox1.List(list_count - 1) & " "
Next
MsgBox a
End Sub


On execution, I selected two rows as shown.



As soon as, I clicked "OK", I got the following Message.


Happy VBA..ing