excel vba listbox

Excel VBA ListBox – Adding Items, Clearing, Multiple Selection

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

The Excel VBA ListBox is a list control that allows you to select (or deselect) one or more items at time. This is compared to the VBA ComboBox which only allows you to select a single items from a drop down list. Let us explore how to create, clear and make a VBA ListBox let you select multiple items.

Create a Excel Form ListBox (Form Control)

To create an Excel Form ListBox you need to first make sure you can see the Developer Tab. Next go to Controls and select the ListBox. Place the ListBox on the Worksheet. You can right click on it to set the properties.
excel vba listbox

Using the Form ListBox

To add items to set items in a From ListBox is easiest by providing an Excel Range on your Worksheet that contain values for options.

Select Excel Range as ListBox Items

To add an Excel Range as Items for a ListBox right-click on the object and go to Format Control. Next go to the Control tab.
Configure a VBA ListBox
As you can see we managed to:

  • Set the Input range for the items in the Form ListBox
  • Set the Cell Link range for the selected item

The Excel Form ListBox is an option to use if you need a simple way to limit input options for your users in an spreadsheet.

Using Form ListBox does not make much sense as just as well we have Data Validation allowing you to set a drop down list to any Excel cell. See my post on Excel Cascading Drop-downs if you want to see how powerful this can be.

Beware in below examples I am using the ActiveX ListBox instead!

Create a VBA ListBox (ActiveX)

Now let us explore the adding Items to a Listbox using VBA. For this purpose it is easier and more convenient to use an ActiveX ListBox control (which I call the VBA ListBox) instead of the previously used Form ListBox:
vba listbox
In examples below I will be adding code to the Worksheet module in which I added a ActiveX ListBox control named ListBox1.
vba listbox name

Add Items to ListBox

To add VBA Add Items to the VBA ListBox use the AddItem function.

Examples of adding items to a ActiveX ListBox with VBA:

The resulting VBA ListBox. Notice that Plane is second in the list:
add items to vba listbox
This list will work identically to the Excel Form ListBox.

Clear items in ListBox

To Clear/Empty items in a VBA ListBox simply use the Clear function:

Remove item from ListBox

To remove an item at a specific index or position in a VBA ListBox you need to use the RemoveItem function. Remember that the index of the first element is 0:

Count items in ListBox

Counting items in a ListBox can be done using the Count function:

Multiple Selection VBA ListBox

To enable multiselection on your VBA ListBox (ActiveX) use the MultiSelect property. The property can have one of the following values:

  • fmMultiSelectSingle – default property. You can only select 1 item
  • fmMultiSelectMulti – you can select multiple items. Clicking on an item will select (include it in the existing selected items) or deselect it (remove it from the existing selected items)
  • fmMultiSelectExtended – you can select multiple items. However, when you click on any item it will only select the current item. To select multiple items you need to click and hold and move the mouse up/down to select more items

By setting the MultiSelect option:

I am able now to select more items on my ListBox.
vba listbox multiselect

Select / Deselect items in ListBox

First we will try to understand how to check if an item on our ListBox is selected or not. For this we will use the Selected property.

To Select or Delect an item simply set the property to True (Selected) or False (Deselected):