Skip to content

VBA Listbox

Associated Files Download Links

In VBA, you can create a Listbox where a user can choose one of the listed options. Listbox is often used in Userforms, but can also be used in a Worksheet. In this tutorial, you will learn how to create, populate and delete a Listbox. You will also see how to get a user choice in VBA and use it in code.

If you want to learn how to create a ComboBox, click here: VBA ComboBox

If you want to learn how to create a Checkbox, click here: VBA Checkbox

 

Create a Listbox

In order to insert a Listbox in the Worksheet, you need to go to the Developer tab, click Insert and under ActiveX Controls choose List Box:

vba-insert-listbox

Image 1. Insert a Listbox in the Worksheet

 

When you select the Listbox which you inserted, you can click on Properties under the Developer tab:

vba-listbox-properties

Image 2. Change Listbox Properties

 

Here you can set different properties of the Listbox. For the beginning, we changed the attribute Name to lstListBox. Now, we can use the Listbox with this name in VBA code.

Populate a Listbox in VBA code

First, we need to populate the Listbox with values. In most cases, a Listbox needs to be populated when the Workbook is opened. Because of this, we need to put a code for populating the Listbox in object Workbook, procedure Open. This procedure is executed every time a user opens the Workbook. Here is the code:

As you can see in Image 3, we populated our Listbox with 5 names (John, Michael, Jennifer, Lilly and Robert):

vba-populate-listbox-in-vba

Image 3. Populate the Listbox in VBA

 

Populate a Listbox from a Cells Range

Another possible way to populate a Listbox is to let a user do it. A Listbox can be linked to the cells range. Therefore, every time a user enters a new value in the cells range, the Listbox will update with that value.

If you want to enable this, you have to go to the Properties of the ListBox and set the attribute ListFillRange:

vba-populate-listbox-from-cells-range

Image 4. Populate the Listbox from the cells range

 

We linked our Listbox with the range E2:E5, where we put names we want (Nathan, Harry, George, Roberta). As a result, the Listbox is now populated with these names.

 

Get a Selected Item of a Listbox in VBA

The purpose of a Listbox is to get a users choice. In order to retrieve a value that is selected by a user, you need to use this code:

The users selection is in the attribute Value of Sheet1.lstListbox object. This value is assigned to the variable strSelectedItem:

vba-listbox-get-selected-value

Image 5. Get a selected value from the Listbox in VBA

 

We selected Harry in the Listbox and executed the procedure. As you can see in Image 5, the value of the strSelectedItem is Harry, which is the value we selected. Furthermore, you can process this variable in the code.

Clear a Listbox

In order to clear a Listbox in VBA, you need to use Clear method of Sheet1.lstListBox object. It will delete all the items from the Listbox. Here is the code:

When we execute the code, we get the empty Listbox:

vba-clear-listbox

Image 6. Clear the Listbox

Use a Listbox in a Userform

As we mentioned, Listbox is most often used in Userforms. To explain how you can do it, we will first insert an Userform. In VBA editor, right-click on Module name, click on Insert and choose UserForm:

vbalistbox insert in userform

Image 7. Insert a Userform

 

To display controls for inserting, you need to enable the Toolbox. To do this, click on the Toolbox icon in the toolbar. After that, you will get the windows with all the controls available. You can click on ListBox to create it in the Userform.

vba insert listbox in userform

Image 8. Insert a Listbox in the Userform

 

We will name the ComboBox cmbComboBox. In order to populate it with values, we need to put the following code into the method Initialize of the object UserForm:

This code triggers every time a user runs the Userform and populates the Listbox with these 5 names:

vba listbox in userform

Image 9. The Listbox with values in the Userform

 

If you want to get selected value from the ComboBox, you need to use the same logic for the Combobox in a Worksheet, which is explained earlier in the article.

 

 

You may also like some of this related content...

VBA Tables and ListObjects This VBA tutorial will show you how to work with Tables
In VBA, you can create AutoFilter in order to filter a range of cells or
In VBA, you can create a Spin button which allows a user to increment a
In VBA, you can create an Option button to enable a user to choose from
In VBA, you can create a CheckBox where a user can check or uncheck the
Advertisements
Automate Excel
Left Menu Icon