Microsoft Office Tutorials and References
In Depth Information
Following are a few points to keep in mind when working with ListBox controls. Examples in
the sections that follow demonstrate many of these points:
h You can retrieve the items in a ListBox from a range of cells (specified by the
RowSource property), or you can add them by using VBA code (using the AddItem
h You can set up a ListBox to allow a single selection or a multiple selection. You use the
MultiSelect property to specify the type of selection allowed.
h If a ListBox isn’t set up for a multiple selection, you can link the value of the ListBox to a
worksheet cell by using the ControlSource property.
h You can display a ListBox with no items selected (the ListIndex property will be –1 ).
However, after an item is selected, the user can’t deselect all items. The exception is if the
MultiSelect property is True .
h A ListBox can contain multiple columns (controlled by the ColumnCount property) and
even a descriptive header (controlled by the ColumnHeads property).
h The vertical height of a ListBox displayed in a UserForm window isn’t always the same as
the vertical height when the UserForm is actually displayed.
h You can display the items in a ListBox either as check boxes (if multiple selection is
allowed) or as option buttons (if a single selection is allowed). The display type is
controlled by the ListStyle property.
For complete details on the properties and methods for a ListBox control, consult the Help system.
Adding items to a ListBox control
Before displaying a UserForm that uses a ListBox control, you need to fill the ListBox with
items. You can fill a ListBox at design time using items stored in a worksheet range, or at runtime
using VBA to add the items to the ListBox.
The two examples in this section presume that
h You have a UserForm named UserForm1 .
h This UserForm contains a ListBox control named ListBox1 .
h The workbook contains a sheet named Sheet1 , and range A1:A12 contains the items to
be displayed in the ListBox.
Adding items to a ListBox at design time
To add items to a ListBox at design time, the ListBox items must be stored in a worksheet range.
Use the RowSource property to specify the range that contains the ListBox items. Figure 14-8
shows the Properties window for a ListBox control. The RowSource property is set to
Sheet1!A1:A12 . When the UserForm is displayed, the ListBox will contain the 12 items in this