• Home  / 
  • Excel
  •  /  Create a Dependent Combobox in Excel and VBA

Create a Dependent Combobox in Excel and VBA

In this blog, we will discuss how to create a Combobox in Excel and as well as through VBA.

List box & a Combobox are similar but have slightly different controls, to let users make multiple choices of items or to enter their own value in a list. Typical examples of items in these types of lists are employee names, exchange rates, and product items.

We all know how to create a validation list in Excel. But very few of us know how to create a dependent list in Excel. For Example, you have the validation list looks like below.

create combobox drop down in excel

(Picture 1)

Now my point is that if I choose “Animals” from the drop down menu, then a list of animal names will be shown in the drop down.

vba excel combobox

(Picture 2)

Related article: Export data from SQL to Excel spreadsheet using VBA

Similarly, from the same box (Picture 1) if I choose “Fruits” the same box will be shown the list of fruit names. To do the same you need to set the name range. I am thinking that your data looks like below.

combobox menu items

(Picture 3)

Now I will create the named range for all of these items are shown in Picture 1. We will give the same name for each and every category. I am choosing all the animals’ name and giving the name as “Animals”.

dependent combo box excel

(Picture 5)

dependent combo box excel

(Picture 4)

Similarly, for the Fruits, Vegetables and for the cell phone I have aerated the name manager and there are 4 names on my list. Remember, we need to give the same name what is there for the first validation you have chosen.
Name Manager for creating comboboxNow we will choose the cell where the dependent list will be created. In my case, my validation list in H2 cell and I am going to dependent validation list on I2. After choosing I2 cell I go Data and then Data Validation from the Data Tools section. Then Choose List from Allow section and in the Source I have written the formula =indirect($H$2). It is the trick. The value of H2 is Animals and we have already set and Animals mean a list of animals’ name.

Data validation for combobox

(Picture 7)

After setting the same as like above please click on OK and your dependent is ready to use. You can choose any particulars from the first validation box and the dependent list will be populated.
ready to use dependent combobox

Related article: 5 Powerful Formatting Tricks in Excel

So your dependency list ready. Only a small formula (indirect) has done the trick. For Indirect formula in details, you can go through our training materials.

Dependent list box

(Picture 9)

Now to do the same thing in VBA is a little bit tricky. I am creating 1 combobox and 1 list box in the user form and the combobox is connected with the list for each category.
Here also, we will do the same the thing. If you choose 1 category from the first box (combobox) then the dependent value in the same list box will be populated. To do the same thing, please follow the below steps for creating the above UserForm.

  1. Insert a new module
  2. Paste the below code
  3. Open UserForm
  4. Double click on the Form
  5. Paste the below UserForm code

Module Code:

The module code should look like below.

module code

(Picture 10)

The UserForm code should look like below

UserForm code

(Picture 11)

Please run the code or press F5 to run the code. You will get the UserForm and choose one category from the Combobox and as soon as we will choose any value, then the dependent value in the List box will be populated.

Dependent list

(Picture 12)

I have selected a Fruits category from the box and the dependent value has now populated.

Hope you have found this as useful. Please download the excel macro code file and play with it.

Now if I tell you to select the 3 fruits from the Listbox and save them in a place how will do you the same? I will cover that part on my next blog.

Please go through our blogs on a regular basis to enhance your knowledge of Excel and VBA.

Do refer your friends to follow our blogs on Advanced Excel VBA tutorials and FREE Excel VBA Tutorials

About the author

Niladri Sekhar Biswas

Hi, I have 9 years of experience working in Microsoft Excel. I am also an Excel and VBA corporate trainer. Writing about Excel and sharing my knowledge is my passion. I hope you learn more about Excel from my articles.