• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Dependent Combo Boxes on Excel VBA Userform

Dependent Combo Boxes on Excel VBA Userform

In this blog post, we will look at how to create dependent combo boxes for your userforms in Excel VBA.

The drop down options in the second combo box are dependent upon the selection made in the first combo box.

We will cover 2 ways of achieving this.

Create a Dependent Combo Box with Named Ranges

In this example we have two combo boxes on a userform.

One with a list of countries, and another with a list of cites. We would like the second list to only show the cities for the country selected in the first list.

Combo boxes on a userform

The first combo box is named cboCountry and the second combo box named cboCity.

The spreadsheet with the data being used by these combo boxes can be seen below.

Data for the dependent combo boxes

Each list has been given a defined name. We can then reference that name within our VBA code in a similar way to my dependent data validation list tutorial.

The VBA code below has been used in the combo box change event for cboCountry, so that whenever a selection is made in that list, the code is triggered.

This code uses the Select Case conditional construct. If you are not familiar with this technique, check out my Select Case tutorial.

Private Sub cboCountry_Change()
Select Case cboCountry.Value
 Case Is = "UK"
 cboCity.RowSource = "uk"
 Case Is = "Spain"
 cboCity.RowSource = "spain"
 Case Is = "New Zealand"
 cboCity.RowSource = "new_zealand"
 Case Is = "Italy"
 cboCity.RowSource = "italy"
 Case Is = "Netherlands"
 cboCity.RowSource = "netherlands"
 Case Is = "Russia"
 cboCity.RowSource = "russia"
End Select
End Sub

The Select Case statement makes it easy for us to test the combo box value multiple times, and provide the correct row source for the second combo box.

Watch the Video – Dependent Combo Boxes

Dependent Combo Boxes with the Advanced Filter

In this second example, we will link two combo boxes together so that when a country is selected from the first combo box, a list of customers in that country is shown in the second combo box.

The userform with the two combo boxes looks like this.

Dependent combo boxes with advanced filter

The first combo box is named cboCountry and the second combo box is named cboCustomer.

In this example, the spreadsheet data looks like below.

In column A we have the list of countries used by the cboCountry combo box. And in columns C and D, the countries with the customers we have in those regions.

Data for the advanced filter technique

We do not have named ranges like in example 1. So this time when a selection is made in combo box 1, we will perform a search for the customers in that country from column C and D.

So in this example the plan is to use the fantastic Advanced Filter of Excel.

The code below is used on the change event for cboCountry combo box.

Private Sub cboCountry_Change()
Dim r As Integer
r = 2
Range("F2").Value = cboCountry.Value
Columns("C:D").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
 "F1:F2"), CopyToRange:=Range("H1:I1"), Unique:=False
cboCustomer.Clear
Do Until Cells(r, 9).Value = ""
cboCustomer.AddItem Cells(r, 9).Value
 r = r + 1
Loop
End Sub

When the selection is made in cboCountry. The value is written to cell F2.

The Advanced Filter is then perform using range F1:F2 as its criteria range. The results of the filter are produced in columns H:I.

The items for the second combo box are then created by using a Do loop and the AddItems method for the combo box.

Watch the Video – Advanced Filter Technique

Related Posts:

  • Moving average in Excel thumbnail
    Moving Average in Excel
  • Custom sort in Excel
    Custom Sort in Excel
  • N Functions in Excel thumbnail
    N Function in Excel
  • SWITCH function feature
    Excel SWITCH Function - Nested IF Function Alternative

Reader Interactions

Comments

  1. asim says

    14 April 2018 at 7:47 am

    it’s very markable!!

    Reply
  2. ERICA ORTIZ says

    10 July 2020 at 11:55 pm

    good afternoon it did not work and I would like your help to know what I am doing wrong or can you give me the example

    Reply
    • Alan Murray says

      23 July 2020 at 8:59 pm

      Sorry to hear that Erica. Did you try both code examples. Check through the code you used, there is probably a small mistake.

      Reply
  3. Kadar Khan says

    9 August 2020 at 2:21 am

    hi,
    i have two columns one contains city names and another contains employee name.
    in user form1 i have two combo boxes.
    in combobox 1 i want to populate unique city names from column 1 and in combobox 2 i want their all the respective employees.
    how can achieve that?
    please help me.

    Reply
  4. mahmoud says

    2 September 2020 at 7:51 pm

    gooooooood

    Reply
    • Alan Murray says

      14 September 2020 at 6:19 pm

      Thank you, Mahmoud.

      Reply
  5. mahmoud says

    16 September 2020 at 9:04 pm

    goooooooooooooood

    Reply
  6. Bartosz says

    6 December 2020 at 7:12 am

    Hi,

    Bit of a side question, any idea if it’s possible to block the changebox for editing, while still having the dropdown option active? I want to prevent user to change the text picked from the list. which in this setting is possible.

    Best,
    Bartosz

    Reply
  7. Bill B says

    2 November 2021 at 6:46 pm

    Hi, I love your tutorial and it helped me do exactly what I was trying to do. I do have one issue though. When writing the values from the advanced filter search into the cells (the Do Until Loop), I’m trying to use a Userform on a different sheet and don’t know how to get it to read back to my sheet where the information is.

    Where you have the following code:
    Do Until Cells(r, 9).Value = “”
    cboCustomer.AddItem Cells(r, 9).Value
    r = r + 1
    Loop

    How can I re-write to include writing to a different sheet?

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·