In this tutorial we'll go demonstrate how to create an Excel VBA Listbox. Listboxes can make your worksheets more usable for your users and yourself. Let's jump right in!
A Listbox is another name for a dropdown where the user can select the value from a list of items.
Note: The Developers tab should be clicked so you can change the name of your listbox.
Code:
Private Sub Flowers_Click()
With Sheet1.Flowers
.AddItem "Rose"
.AddItem "Daffodil"
.AddItem "Sunflower"
.AddItem "Babys Breath"
End With
End Sub
Here's a quick explanation of code above:
Private Sub Flowers_Click()
End Sub
With Sheet1.Flowers
.AddItem "Rose"
.AddItem "Daffodil"
.AddItem "Sunflower"
.AddItem "Babys Breath"
Below is a screenshot of the result after running the code:
If you want to delete your text inside your list box, we need to add the following:
Flowers.Clear
Flowers is the Listbox's name plus .Clear to use the clearing function on the list box.
Code:
Private Sub Flowers_Click()
With Sheet1.Flowers
.AddItem "Rose"
.AddItem "Daffodil"
.AddItem "Sunflower"
.AddItem "Babys Breath"
End With
Flowers.Clear
End Sub
See the result below after adding the clear function:
Using a list box is also useful when you want to place a user form.
Code:
Private Sub example2()
Dim strSelectedItem As Variant
strSelectedItem = Sheet1.Flowers.Value
MsgBox strSelectedItem
End Sub
Code explanation:
On the result below rose returned on the message box because we have selected rose.
You can see the result below:
But if we have selected sunflower this would be the result.
Code:
Private Sub example2()
Dim strSelectedItem As Variant
strSelectedItem = Sheet1.Flowers.Value
MsgBox strSelectedItem
End Sub
You can see the result below:
Below is a list of some of the properties for a ListBox.
Below are some of the methods used in creating a Listbox:
This has been a quick tutorial about creating listboxes in Excel using VBA. We hope that this helps you create more usable worksheets. Thanks for joining!