Using Multiselect Listboxes

Listboxes from the Control Toolbox (also called ActiveX Listboxes) have a property called MultiSelect. This property allows the user to select more than one item in the listbox. Valid settings for this property are:

0 - fmMultiSelectSingle: User can only select one item at a time.
1 - fmMultiSelectMulti: User can click or press the spacebar to select and deselect items.
2 - fmMultiSelectExtended: User can use SHIFT and CTRL to select multiple items.

To process selected items in a MultiSelect listbox, you need to cycle through the items and use the Selected property of the ListBox object. An example:

Sub ShowSelected()
    Dim i As Long
    Dim Msg As String
    
    With Sheet1.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Msg = Msg & .List(i) & vbCrLf
            End If
        Next i
    End With
    
    If Len(Msg) = 0 Then
        Msg = “No items selected”
    End If
    
    MsgBox Msg
End Sub

I put most of this sub in a With Block. It speeds up the code and helps readability. The For Next loop loops through all the items in the listbox. The first item is numbered 0 and the last item is numbered ListCount-1. The Selected property is an array with the same number of elements as there are items in the listbox. It contains either True, if that particular item is selected, or False, if not.

If Selected(i) is True, then the item is concatenated with the variable Msg (and a new line) to be shown later. If it’s False, nothing happens and the loop goes to the next item. The last If Then tests Msg to see if anything was selected and the MsgBox function is used to display the selected items or another message if nothing was selected.

The item that is concatenated to the variable is accessed with the List property. Like Selected, List is an array that has the same number of elements as there are items in the listbox. Unlike Selected, List contains the value of the items. Incidentally, List also has a column argument. The example above assumes a single column listbox, so the column argument isn’t used.

5 Comments

  1. Eugene Nae:

    I notice that when you use MultiSelect Extended or Multi, Excel wipes out your selections. I link the list to an Excel range, select some items, Save, open the workbook again and nothing is selected. Please help if you know a workaround.

  2. Eugene nae:

    FIGURED IT OUT!!!

    You use Shape.OLEFormat.Object to get to the original ActiveX object from the Excel control. From then on, it acts as a normal ActiveX control. (use ListBox.Selected(index) = False/True)

  3. supergoat:

    Eugene/Dick,

    Can you explain a litter further how i ensure that items selected in a multi-select listbox are saved and retrieved when the worksheet is reopened.

    Thanks

  4. Dick Kusleika:

    supergoat: Check out this post

    http://www.dicks-blog.com/archives/2004/06/29/preserving-listbox-selections/

  5. Mui Hua:

    I’ve a ListBox with the setting of MultiSelectExtended. I need to point the selected data to a series of cells in another spreadsheet for my further action. So far, I am only able to link to 1 cell but it’s not what I wanted. Can you help?

Leave a comment