VBA ComboBox Excel Macros Examples Codes for Adding new Items,Adding new Items to another ComboBox based on selection of first ComboBox ,Clearing Tutorials. ComboBox in Excel VBA is one of most useful control in the Excel. You can show the list of items in the ComboBox and user can select any one item and do different operations. In this tutorial, we will explain different example on using ComboBox.
ComboBox in Excel VBA – Example Cases:
- Add Items to ComboBox while opening Workbook
- Add Items to ComboBox2 based on ComboBox1 selection
- Get data to TextBox based on ComboBox2 selection
- Clear ComboBox Items
- DownLoad:Example File
Sample ComboBox Design:
- GoTo Developer Tab from Menu
- GoTo Insert from Controls Part
- Insert two ComboBox’s and two TextBox’es from ActiveX Controls
- The final design should be as shown below
Screen Shot:
Add Items to ComboBox while opening Workbook
You can Add items to the ComboBox while opening the Excel Workbook. The following example will show you how to populate the items in ComboBox while opening excel file.
Code:
Private Sub Workbook_Open() Application.EnableEvents = False 'Clear ComboBox1 Items Call Clear_ComboBox 'Add Items to ComboBox1 in Sheet1 while opening workbook With Sheet1.ComboBox1 .AddItem "Fruits" .AddItem "Vegetables" .AddItem "Soaps" .AddItem "Beverages" End With Application.EnableEvents = True End Sub
Output:
Here is the screen-shot of the ComboBox1 with items.
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- From Project Explorer Double Click on ThisWorkbook
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook and Close it
- Open the file to see the output
- You should see the Items in ComboBox1 as shown above
Add Items to ComboBox2 based on ComboBox1 selection
You can add items to ComboBox2 based on ComboBox1 selection. It is helpful while developing tools. You can provide the user to select item from first ComboBox and add items to second comboBox based on first ComboBox Selection.Please find the following example below.
Code:
'Get Items to ComboBox2 based on ComboBox1 selection Private Sub ComboBox1_Change() 'Variable Declaration Dim iCnt As Integer 'Clear Combobox2 before loading items ComboBox2.Clear With ComboBox2 Select Case ComboBox1 Case "Fruits" .AddItem "Apple" .AddItem "Pomegranate" .AddItem "Grape" .AddItem "Pineapple" .AddItem "Gouva" Case "Vegetables" .AddItem "Tomato" .AddItem "Brinjal" .AddItem "Radish" .AddItem "Potato" .AddItem "Onion" Case "Beverages" .AddItem "Pepsi" .AddItem "Limca" .AddItem "Miranda" .AddItem "Sprite" .AddItem "Coco Cola" Case "Soaps" .AddItem "Lux" .AddItem "Rexona" .AddItem "Dove" .AddItem "Lifeboy" .AddItem "Liril" End Select End With End Sub
Output:
Here is the screen-shot to show you adding the items to second ComboBox based on first ComboBox Selection.
Instructions:
- Please follow the above mentioned design steps
- Goto Developer Tab from the menu, Click on Design Mode in the Sheet1
- Double Click on ComboBox1
- Copy the above code and Paste in the code window
- Goto Developer Tab from the menu, Click on Design Mode in the Sheet1
- Now select Item from Combox1. Now you can see the items in Combox2 based on ComboBox1 selection
Get data to TextBox based on ComboBox2 selection
The following example will show you how to get data to TextBox based on ComoBox2 selection.
Code:
'Get Price based on ComboBox2 selection Private Sub ComboBox2_Change() 'Variable Declaration Dim iRow As Integer iRow = 1 'Clear Combobox2 before loading items TextBox1.Text = "" 'Get Price based on ComboBox2 selection Do iRow = iRow + 1 Loop Until ComboBox2.Text = Sheets("Data").Cells(iRow, 3) TextBox1.Text = Sheets("Data").Cells(iRow, 4) TextBox2.Text = Sheets("Data").Cells(iRow, 5) End Sub
Output:
Here is the sample screen-shot.
Instructions:
- Please follow the above mentioned design steps
- Goto Developer Tab from the menu, Click on Design Mode in the Sheet1
- Double Click on ComboBox2
- Copy the above code and Paste in the code window
- Goto Developer Tab from the menu, Click on Design Mode in the Sheet1
- Now select Item from Combox1 & Combox2. Now you can see the Price and availability of item to the Textbox as aoutput based on ComboBox2 selection
Clear ComboBox Items
You can clear the ComboBox using Clear method. The following procedure will show how to clear the ComboBox items, this procedure will clear the ComboBox items before loading an items to ComboBox.
Code:
Sub Clear_ComboBox() 'Clear ComboBox & TextBox data With Sheet1 .ComboBox1.Clear .ComboBox2.Clear .TextBox1.Text = "" .TextBox2.Text = "" End With End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- We can call this procedure to clear ComboBox items before loading items to ComboBox
- It will clear items from ComboBox
Example File
Download the example file and Explore it.
ANALYSISTABS – Combo Box
I’d like to do the same thing as above with the first two comboboxes, but instead of using item hard-coded into the programming, use group of cells, so, where you see
Select Case ComboBox1
Case “Fruits”
.AddItem “Apple”
.AddItem “Pomegranate”
.AddItem “Grape”
.AddItem “Pineapple”
.AddItem “Gouva”
i’d like it to refer to cells that have those values in them, and tell it to keep looking untill it finds a blank cell (meaning theres no more values) so i can feel free to add and delete values as i want, right in the sheet. i’m sure this is possible, and i’ve seen it done, but can someone show me using the above example?
thanks!
Hi Jared,
Here is the solution.
Assuming that you have the following data in Column A and B:
Here is the code:
Private Sub ComboBox1_Change()
Dim iCntr As Integer
iCntr = 1
'Looping until column A is blank
ComboBox2.Clear
Do While Sheets("Sheet1").Cells(iCntr, 1) <> "
'Checking if the column a is equals to ComboBox1 selected value
If Sheets("Sheet1").Cells(iCntr, 1) = ComboBox1.Value Then
'Adding items to ComboBox2, respective items in the Column B
ComboBox2.AddItem Sheets("Sheet1").Cells(iCntr, 2)
End If
iCntr = iCntr + 1
Loop
End Sub
Hope this helps.
Thanks-PNRao!
Hi,
I want to add quantity column and want a Total Quantity field in Combo Box with capability to auto sum quantity of Apple.(Suppose we have entered Apple in Row 2 as 100 Qty and Row 5 as 50 Qty in Data sheet.) And when anybody selects Fruit>Apple it will display Quantity as 150. This new part i want to add keeping rest of thing same. Need your help….
Hi Amith,
You can loop through the rows and check for the ‘apple’ and the add the respective qty. For example:
in FruitComboBox_change()
[vb]
‘Assuming Fruit Name in Column A and Qty in Column B
If Trim(FruitComboBox.Value) <> "" Then
lRow = 200 ‘Your last row in data sheet
totQty = 0
For iCntr = 1 To lRow
If Sheets("Data").Cells(iCntr, 1) = FruitComboBox.Value Then totQty = totQty + Sheets("Data").Cells(iCntr, 2)
Next
‘Display the final Qty
QtyTextBox = totQty
End If
[/vb]
Hope this helps!
Thanks-PNRao!
Dear sir,
I would like to know that how i linked different cells rows with the same combobox which populated in different textboxes. kindly help.. Regards, Bhawesh
good tutorial and well defined example
Thank you Sharmila! We are glad we could help and hearing good feedback from our readers.
-PNRao!
Thank You! Very Perfect.
Hi!
this is a good and easy to understand example.
the query i have in my mind is that..
i have a vb program and an excel sheet separately and i have linked it thru OLEDB connection.
i have two combo boxes and i have to read data from excel sheet .
how do i do that ?
Good. This helps me a lot. Thanks.
Thank you klorvalex! You are most welcome to our blog. – PNRao!
You can get the data from Excel to an arry and assign to ComboBox:
Example:
ComboBox1.List=ArrData
Thanks-PNRao!
You are most welcome Farbood! We are very glad and happy to hear such a nice feedback.
Thanks-PNRao!
Your explanations and templates are excellent .Thanks a lot
Thanks for the feedback!
Regards-PNRao!
Hi guys, am stuck somewhere, av been able to create 2 combo boxes in my form and populated the first one. my question is how can i link the two combo boxes so that the list is combo box 2 depends with the user’s selection in combo box one, which fetches data from an excel worksheet.
Any help?..Thanx in ADVANCE …
THIS IS MY CODE (for populating combo box 1)
Assuming your you have two combo boxes on user form named ComboBox1 and ComboBox2:
Hope this helps, Thanks-PNRao!
Hi PNRao,
Hope you are doing good.
a very small query. i have a code in which i am taking the input through input box and those values are getting stored in defined cells.what i want to do is the values that i am taking as an input if any of the values is less then a particular value say suppose 50 then that particular cell will return the value with a color red.
i was doing the following but its not executing anything.
Dim i As Integer
i = 1
Do While Cells(i, 2).Value ”
If Cells(i, 2).Value < 50 Then
Cells(i, 2).Font.Color = vbRed
i = i + 1
End If
Loop
help would be much appreciated.
I just can’t create a combobox code that extracts its items direct from a worksheet dependind on the value related to a particular cell. Something like ‘if “a1=2” then “items are this column” else “items are that column”‘.
Any help will be useful. Thanks.
Hi Roy,
increment statement should be out side the if block:
Thanks-PNRao!
Hi Marcel,
Please check this code:
http://analysistabs.com/excel-vba/combobox-adding-clearing-items/#comment-37171
Thanks-PNRao!
Thanks a Lot, It will pay you for your kind support
Hi,
Sir I am a new learner of vba. so could you please uploade the data file of above example. so that i can understand.
Regards
Bhupender Singh
Big fan of yours tutorial
i want to select item’s quantity from combo-box and calculate price and it will input in textbox using Visual Basic Studio
Hi guys can you please help me? I want select item’s quantity from combo-box and calculate price.
hi,
iam a new to vba code..i have an excel sheet in that I create dropdown list..like
IF process is main then the list items are
timeline
kaizen
datatabel
fifolane
when ever I will select the drop down list value according to that in next cell data will come autopopulate… can you send the code in vba
‘Get Price based on ComboBox2 selection
Do
iRow = iRow + 1
Loop Until ComboBox2.Text = Sheets(“Data”).Cells(iRow, 3)
TextBox1.Text = Sheets(“Data”).Cells(iRow, 4)
TextBox2.Text = Sheets(“Data”).Cells(iRow, 5)
End Sub
can you explain this code….
here data in the sense wat…can you pls explain this…this will helpful for me…
im unable to download the file
Updated the link, please download the file.
Thanks-PNRao!
please upload example excel file.
Dear Sir,
Excellent tutorial. I need help with intermediate and advanced excel tutorials. Will you be able to provide tutorials in a structured course format. Thanks
Hi. I am stuck at a place where I am selecting the value from the first Combo box and according the value is listed in the second Combo box. Now the challenge for me here is the values of the second combo box is too long , during display the value is left aligned which is easier for the user to select the item but the once the value is selected the value is displayed right aligned which is difficult to identify what was selected. How can I show the value initially and on selecting also the value is left aligned.
Kindly help.
I downloaded this code and modified it for my 2 boxes and it works perfectly – thank you!!!
Now, I have a project where I need to add a 3rd dependent box. Sticking with the .additem way, how Shoukd I wrote the code to get box 2 items to box?
Thank you so much! I’m struggle with the combinations.
Hello Sir,
I want to know how to make a combo box on a userform with search suggestions.
The combox shall have items in it already from which i can choose one. However if the no. of items are too many , then in that case i may write the item name and its suggestion shall be reflected.
like if i type ” Ram” in the combobox then i shall get the suggestion like ” Ramesh” & ” Ramalingam” etc.
Kindly help me in this .
Hi…
Thanks for your tutorial.
I get one problem.
After coding as above i get result but after closing excel sheet program need to run manually.
It doesn’t work with auto run.
Hiya,
Would it be possible to use Loop to identify a group of combo box? For example :
For i = 1 to 10
Sheets(“Sheet1”).combobox(i).List = Array(“Apple”, “Banana”, “Coconut”)
next i
Thank you