ComboBoxes and listboxes are useful approaches to controlling the consumer experience by confining the values that a consumer can choose to a predefined list. If you start a ComboBox or listbox, the value will be blank by default. But you might need to select the first item in the list. To do that, you can use a simple VBA code. In this article, we will show you how to select the first item from ComboBox using VBA in Excel with easy steps.
How to Select First Item from ComboBox Using VBA in Excel: 5 Easy Step
In this article, we will demonstrate 5 easy steps to select the first item from ComboBox using VBA in Excel. Keep on reading to learn how to do it.
Step 1: Create Dataset with Proper Parameters
In the first step, you will have to create your list. Here, we created a “List of Food” column with 8 items and another column with “Quantity Sold.” Then we created a ComboBox with the list of the first column. As a result, our dataset looks like this:
Step 2: Open Microsoft Visual Basic Window
In this step, we will open the Microsoft Visual Basic window.
- First of all, go to the Developer tab.
- Then press on Visual Basic from the ribbon.
- You can also apply the keyboard shortcut Alt+F11 to open the window.
- The Microsoft Visual Basic window will appear.
Step 3: Create ComboBox
Now we need to insert a new Userform and create a ComboBox.
- To do so, go to the Insert tab and select Userform.
- A Userform window will open with a Toolbox. From the Toolbox, click on Combobox.
- Now drag your cursor to create a ComboBox of your desired size.
- Next, double-click inside the combobox to open a window to write codes.
Step 4: Copy-Paste VBA Code
After that, we will need a VBA code to select the first item from the ComboBox.
- Write the following VBA code or just copy it from below and paste it to the window.
Private Sub ComboBox1_Change()
Me.ComboBox1.AddItem "Cookies"
Me.ComboBox1.AddItem "Chocolate"
Me.ComboBox1.AddItem "Coffee"
Me.ComboBox1.AddItem "Yogurt"
Me.ComboBox1.AddItem "Donuts"
Me.ComboBox1.AddItem "Croissant"
Me.ComboBox1.AddItem "Waffles"
Me.ComboBox1.ListIndex = 0
End Sub
- If you want to change the name of the Userform, go to Properties and change the Caption.
Step 5: Run VBA Code
Finally, it’s time to run the VBA code.
- Click on the Run Macro button or just press F5 on your keyboard to run the code.
- Now, the worksheet will open and you will find a ComboBox.
- Type anything in the combobox. It will just show the first item of the list.
- Again, if you try to select other items, it will still show the first item.
- Make sure to insert the Userform instead of Module in the Microsoft Visual Basic window.
- Write the item you want to be selected in the first place in the VBA code.
Read More: How to Use ComboBox to Get Selected Item Utilizing VBA
Download Practice Workbook
Download this practice workbook for practice while you are reading this article.
Concluding Remarks
Thanks for making it this far. I hope you find this article useful. Now you know select the first item from combobox using VBA in Excel. Please let us know if you have any further queries and feel free to give us any recommendations in the comment section below.