How to determine the items that are selected in a ListBox control

For a Microsoft Word 98 Macintosh Edition version of this article, see 201669 .

Summary

This article describes how to retrieve selected items from a
ListBox control that makes it possible for you to select multiple values.

More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

In a UserForm, when you set the MultiSelect property to 1 - fmMultiSelectMulti for a ListBox control, you can select any number of items from a list. For example, if a list contains the days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday), you can select any, none, or all the items.

To determine the items that are selected, you can use the Selected property of the list box. The Selected property of a list box is an array of values where each value is either True (if the item is selected) or False (if the item is not selected).

For example, if the list contains the seven days of the week and Sunday, Tuesday, and Saturday are selected, the Selected property array would contain the following values:
True, False, True, False, False, False, True
This is true because the first item (Sunday) is selected, the second item (Monday) is not selected, the third item (Tuesday) is selected, the fourth through the sixth items (Wednesday, Thursday, and Friday) are not selected, and the seventh item (Saturday) is selected.

Sample Visual Basic procedure

The following macro code provides two methods for using the selected data from the ListBox. The first method uses one selected item at a time, and the second method builds a list of all the selected items.

Note These steps for creating a UserForm in the Visual Basic Editor assume that you have an understanding of Visual Basic for Applications, Microsoft Word, and Microsoft Forms design and tools.
  1. Start the Visual Basic Editor. To do this, press ALT+F11.
  2. If the Properties dialog box is not visible, click Properties on the View menu.
  3. If the Project Explorer window is not visible, click
    Project Explorer on the View menu.
  4. On the Insert menu, click
    UserForm.
  5. Click the ListBox control on the
    Controls Toolbox, and then drag it to the UserForm.
  6. In the Properties-ListBox1 window, change the MultiSelect property to the 1 - fmMultiSelectMulti value.
  7. Click the CommandButton control on the
    Controls Toolbox, and then drag it to the UserForm to put the CommandButton1 control on the UserForm.
  8. Repeat step 7 to put a second
    CommandButton on the UserForm. This puts the
    CommandButton2 control on the UserForm.
  9. Double-click the UserForm to display the Code window for the UserForm.
  10. Press PAGE DOWN, and then type the following macro code for the Userform_Initialize and the CommandButton_Click events:
Private Sub UserForm_Initialize()

'Creates and assigns the Array to the ListBox when the form loads.
Dim mylist As Variant

mylist = Array("Sunday", "Monday", "Tuesday", "Wednesday", _
"Thursday", "Friday", "Saturday")
ListBox1.List = mylist

End Sub

Private Sub CommandButton1_Click()

'First Method: Displays individual selections one at a time.
For x = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(x) = True Then
MsgBox ListBox1.List(x)
End If

Next x
Unload Me

End Sub

Private Sub CommandButton2_Click()

'Second Method: Displays all the items in one message box
Dim msg As String

For x = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(x) = True Then
msg = msg & ListBox1.List(x) & vbCrLf
End If

Next x
MsgBox "You have selected " & vbCrLf & msg
Unload Me

'Uncomment the following two line to insert the variable
'into a document.
'Documents.Add
'Selection.TypeText Text:= msg

End Sub
Egenskaper

Artikel-id: 813150 – senaste granskning 25 apr. 2008 – revision: 1

Feedback