VBA List Box

What Is List Box In Excel VBA?

VBA List Box in Excel is a list assigned to a variable. The VBA List Box contains the list of items given by a user which allows other users to select the desired values from the list box as part of the automation of reports in Excel.

For example, if we want a user to select months from a list, we can create a list box in Excel with the help of VBA code and get the input values from the users. This article will be a complete guide for VBA list box.

Key Takeaways
  • The VBA List Box is a list assigned by a user which allows other users to select the desired values.
  • There are two kinds of list boxes i.e., Form Controls List Box and ActiveX Controls List Box.
  • By default, the user can select only one item from the VBA list box.
  • By entering the list box name, we can choose the list box in the VBA code.

How To Create A List Box In VBA?

The list box is available under the Developer tab in Excel.

Vba List box - Developer tab

There are two kinds of VBA list boxes available i.e., Form Controls List Box and ActiveX Controls List Box. Since we are talking about the VBA list box, we will talk only about the ActiveX Controls list box.

Step 1: To create a list box, first, go to the Developer tab.

Next, under the Insert option, click on ActiveX Controls List Box.

Vba List box - Create box Step 1

Step 2: Once we click on the list box, we must draw this on our worksheet.

Vba List box - Create box Step 2

As we can see, we have got a rectangle box. Now, to get values on the list box, we need to design the list box.

Step 3: Next, right-click on the list box and then, click on Properties.

Vba List box - Create box Step 3

This will bring the properties window as shown in the following image:

Vba List box - Create box Step 4

We will not be using all the properties. However, we will take you through some of the important properties of the list box.

Name – In this property, we can give a name to the VBA list box. Now, give a name that we can easily access in the list box.

Vba List box - Create box Step 5

Once we give a name to the list box, we will see the same name in the name box in excel whenever the list box is selected.

Vba List box - Create box Step 6

Back Color – In this property, we can change the background color of the list box.

Next, click on the drop-down and then, choose the background color as per our wish.

Vba List box - Create box Step 7

Border Color and Border Style – Using this property, we can change the border color of the list box and the style.

Vba List box - Create box Step 8

Font – Here, we can give different font names.

Vba List box - Create box Step 9

List Fill Range – Here, we can give the values to be displayed in the list box. For example, we have the day names in cells A1 to A7.

Vba List box - Create box Step 10

To get these values on the list box, we can give this cell range address under the list fill range.

Vba List box - Create box Step 11

As soon as we give the input range, we will get the list of items on the list box as shown in the below image:

Vba List box - Create box Step 12

Multi Select – Here, we can allow the user to select a single value or multiple values.

Vba List box - Create box -Multi Select

List Style – Here, we can design the list of items. Here we can select either plain or style. With plain, we get the default list of box items.

Vba List box - Create box -List Style

Next, choose the style option, and we will get the following list box style.

Vba List box - Create box -list box style

This is the way of creating a list box in the worksheet.

Creating A List Box Using VBA

However, the process of creating a list box using VBA is slightly different.

Assume we have given only the name to the list box, and we have a blank list box like the following.

Vba List box - Create box -blank box

Step 1: To insert values in the list box, we can write a VBA code. Double-click on the list box, which will open the Visual Basic editor window with the following private sub-procedure auto-populated.

Vba List box - Create box - VBA Code

Step 2: To access the list box, we can enter the list box name that we have given under the Properties settings. Start typing the name My_List_Box and we can see the IntelliSense list showing this name.

Vba List box - Create box -My_List_Box

Step 3: Next, choose the list box and then, enter a dot to see the properties and methods available with the list box.

Vba List box - Create box -methods

Step 4: Since we are trying to add values to the list box, choose the AddItem method.

Vba List box - Create box -AddItem

Now, enter the required value in double quotes.

Vba List box - Create box -Value

Step 6: Let’s run this code, and we should see a given value on the list box.

Vba List box - Create box - run

Step 7: Now, to add multiple values, we need to write multiple lines of code. However, we can give the reference to the cells by using the List File Range property.

Vba List box - Create box -List Fill Range

Step 8: Next, choose this property, enter an equal sign, and then, provide the cell address as A1:A7 in double-quotes.

Vba List box - Create box - property

Now, when we run this code, we will get the list of values on the list box from the excel cell reference A1:A7.

Vba List box - Create box -final output

How To Create A List Box On A UserForm?

The VBA List Box is created on top of the user form in VBA; without creating a user form, we cannot access the list box.

Step 1: First, go to the Insert tab in the Visual Basic Editor window and then, click on User Form.

Vba List box -User Form 1

As soon as we click on the user form, it will insert a user form like the following along with the toolbox.

User Form 2

Now, from the toolbox, we can insert a list box on the user form.

Step 2: Next, click on the list box from the toolbox.

User Form 3

Step 3: After selecting the list box from the toolbox, draw this on the user form.

User Form 4-tool box

Step 4: Once the list box is inserted, we can access the properties of this list by pressing the F4 key.

User Form 5

This properties window is similar to the previous properties window. Now, we can play around with the properties to see the functionality of the list box.

Step 5: Let us show the VBA coding to play with the list box.

Next, double-click on the user form (not on the list box), and we will see the following auto-sub procedure.

User Form 6

This auto-initiated macro is used whenever the user form is clicked. We need to insert values into the list box.

Step 6: Next, copy the following code and then, paste it into the Visual Basic Editor window.  

Private Sub UserForm_Click()       

UserForm1.ListBox1.AddItem “Monday”

UserForm1.ListBox1.AddItem “Tuesday”   

UserForm1.ListBox1.AddItem “Wednesday”   

UserForm1.ListBox1.AddItem “Thursday”   

UserForm1.ListBox1.AddItem “Friday”   

UserForm1.ListBox1.AddItem “Saturday”   

UserForm1.ListBox1.AddItem “Sunday”

End Sub

The above code will insert the values to the list box whenever we click on the user form.

Step 7: When we run this code, it will bring us the blank user form and list box like the following.

User Form 7

To insert values on the list box, click on the user form. Then, we will see values inserted in the list box.

User Form 8

Alternative Code:

Instead of using the list box name to insert a value in every line of the code, we can use the WITH statement to use the user form and list box once and add the line of the item by line. The following code will insert the values into the list box.

Private Sub UserForm_Click()       

With UserForm1.ListBox1   

.AddItem “Monday”   

.AddItem “Tuesday”

.AddItem “Wednesday”

.AddItem “Thursday”

.AddItem “Friday”

.AddItem “Saturday”

.AddItem “Sunday”

End With

End Sub

Store the Value from the List Box

Whenever the user selects the value in the list box, we need to store that value somewhere in the worksheet.

Next, double-click on the list box, and we will see an auto-sub procedure.

User Form 9

Now, add the following code for the above procedure.

Private Sub ListBox1_Click()        

Range(“A1”).Value = UserForm1.ListBox1.Value

End Sub

Whenever the user selects something from the list box, it will store that value in cell A1. For example, let’s run this code, and we will see a blank list box like the following.

User Form 10

Next, click anywhere on the user form, and values will populate the list box.

User Form 8

Now, select any of the values from the list box, and the same value will be stored in cell A1.

User Form 11

We have selected the value Wednesday, and the same value is stored in cell A1.

How To Add A Dynamic Control To List Box?

The List Box was created manually in the user form in the above code. However, we can automate the process of inserting a list box by having a dynamic list box.

Step 1: First, create a blank user form like the following:

Dynamic Control -blank user form

Step 2: Next, insert a command button on the user form from the toolbox.

Dynamic Control -command button

Step 3: Now, right-click on the command button and then, click on Properties.

Dynamic Control -Properties

Step 4: This will open the property window. Give the caption as Insert List Box.

Dynamic Control -Caption

As we change the caption, we see the same name in the command button.

Step 5: Next, double-click on the command button, and we will see the following auto-initiate procedure.

Dynamic Control -procedure

Step 6: In this procedure, add the following code.

Sub Add_Dynamic_Listbox() 

‘Add Dynamic List Box and assign it to object ‘LstBx’ 

Dim LstBx As ListBox 

Set LstBx = UserForm3.Controls.Add(“Forms.ListBox.1”)’’

”List Box Position 

LstBx.Left = 20 

LstBx.Top = 10 

End Sub

How To Add Items To List Box Control In VBA?

To add items to the list box control, we can write a VBA code or make use of the properties window of the list box.

Step 1: First, enter the required values in the range of cells.

Add items - value

Step 2: In the Visual Basic Editor window, select the list box created on top of the user form. Next, press the properties shortcut key F4 to bring the following properties window.

Add items - userform

Step 3: Next, scroll down in the properties window and in the row source, and give the cell address where we have values to be shifted to the list box i.e., range A1 to A7.

Add items - rowsource

As soon as we provide the cell range, we can see the values appear in the list box.

Add items - appear

By Using Named Range

Providing the range of cells is not a good practice; to make a list dynamic, we need to use a named range in excel.

Step 1: To begin with, select the range of cells, A1 to A7 and give it a name in the name box, “Week_Days”.

Named Range-Week_Days

Step 2: Now, in the row source property of the list box, enter this named range name.

Named Range-Rowsource

Using The VBA Code

Apart from the above two methods, we can write the VBA code to insert values into the list box.

To insert a value to the first, we need to access the list box by the name of the list box like the following.

VBA code-name

After entering the list box name, enter the dot and access the add item method.

VBA code-Additem

Next, enter the value in double quotes.

VBA code-Monday

Similarly, write another line of code to insert another value.

VBA code-Tuesday

Like this, we can add values to the list box by writing the code.

How To Clear Items From List Box Control Using VBA?

To clear items from the List Box, we need to either use the properties or write a VBA code. We can use the clear method of the list box in VBA to clear items from it.

clear

This will clear the items from ListBox1.

The Constants Of List Box In VBA

In the multi-select property of the list box, we have 3 constant options, and they are:

constant
  • 0 – fmMultiSelectSingle
  • 1 – fmMultiSelectMulti
  • 2 – fmMultiSelectExtended

Let us explain these 3 constants in detail.

  • 0 – fmMultiSelectSingle – If we choose this option, users can select only one item from the list box.
  • 1 – fmMultiSelectMulti – If we use this option, we can allow the user to select multiple values from the list box.
  • 2 – fmMultiSelectExtended – This is a special feature where we can allow the user to choose a value by using the SHIFT key and clicking on the mouse button or using the arrow key to select or deselect the values from the list box.

Important Things To Note

  • When we draw the list box on the worksheet, we need to use the list fill range property to insert values. But when we use the list box on the user form, we need to use the row source property to insert values.
  • In the list fill range or row source, we need to enter the cell address without double quotes.
  • When we draw the ActiveX Controls List Box on the worksheet, we need to uncheck the design mode and then use the list box.
  • We can change the background color of the VBA list box by using the background property of the list box.

Frequently Asked Questions

1. How to search in the list box VBA?

To create a search box in the list box to display matching results, we need to insert a text box to type the words and then write advanced VBA code to match the results.

2. How to edit the list box in Excel VBA?

To edit the values of the list box, we need to change the input range values or edit the code to insert the values.

3. How do I create a multi-column list box in Excel VBA?

To create a multi-column list box, we need to change the column count property from 1 to the number of the required number of columns.

column count

4. Why is the VBA list box not working?

VBA list box won’t be working if the name given to the list box is different and using the name in the code is different.

Download Template

This article must be helpful to understand the VBA list box, with its formula and examples. You can download the template here to use it instantly.

This has been a guide to VBA List Box. Here we explain how to create, add and clear list box in excel with the help of VBA code and downloadable excel template. You may learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *