+ Reply to Thread
Results 1 to 6 of 6

Dependant combo box (country and city case)

  1. #1
    Registered User
    Join Date
    07-07-2022
    Location
    London
    MS-Off Ver
    2013
    Posts
    16

    Question Dependant combo box (country and city case)

    Hello,

    I need help in a very simple schema in a userform. What i need to do is to set up 2 comboboxes, the first one shows the countries, and the second one shows the cities of the countries above. (I want the correct cities to show up when i select the country above)

    I have just 2 countries, that i can easily show with "row source" but im struggling making the cities combobox appear. I tried 3 different ways but its still not working.

    PHP Code: 
    Dim colonne As Integer
    Dim i 
    As IntegerAs Integer

    Private Sub Userform_initialize()
    colonne 2
    Sheets
    ("L1").Range("B2:C2").Interior.ColorIndex Clear
    Do While Sheets("L1").Cells(2colonne).Value <> ""
    Userform.cboCountry.AddItem Cells(2colonne).Value
    colonne 
    colonne 1
    Loop
    End Sub

    Private Sub cboCountry_Change()
    2
    Userform
    .cboCity.Clear
    Sheets
    ("L1").Range("B2:C2").Interior.ColorIndex Clear
    Do While Sheets("L1").Cells(2colonne).Value <> ""
    If Cells(2i).Value cboCountry.Value Then
    Cells
    (2i).Select
    ActiveCell
    .Interior.ColorIndex 32
    colonne 
    ActiveCell.Column
    End 
    If
    1
    Loop
    3
    Do While Cells(jcolonne).Value <> ""
    Userform.cboCity.AddItem Cells(jcolonne)
    1
    Loop
    cboCity
    .ListIndex 0
    End Sub 

    PHP Code: 
    Dim colonne As Integer
    Dim i 
    As IntegerAs Integer

    Private Sub Userform_initialize()
    colonne 2
    Sheets
    ("L0").Range("B2:C2").Interior.ColorIndex Clear
    Do While Sheets("L1").Cells(2colonne).Value <> ""
    Userform.cboCountry.AddItem Cells(2colonne).Value
    colonne 
    colonne 1
    Loop
    End Sub

    Private Sub cboMarque_Change()
    With L0
    Col 
    = .Rows(2).Cells.Find(cboCountry.ValueLookAt:=xlWhole).Column
    cboCity
    .Clear
    cboCity
    .List = .Range(.Cells(3Col), .Cells(.Cells(Rows.CountCol).End(xlUp).RowCol)).Value
    End With
    End Sub 

    PHP Code: 
    Private Sub UserForm_Initialize()
    With cboCountry
    .AddItem "Canada"
    .AddItem "USA”
    End With
    End Sub

    Private Sub  cboCity_Change()
    Dim index As Integer
    index = cboCity.ListIndex
     cboCity.Clear
    Select Case index
    Case Is = Canada
    With  cboCity
    .AddItem "
    Toronto"
    .AddItem "
    Ottawa"
    .AddItem "
    Montreal"
    .AddItem "
    Vancouver"
    End With

    Case Is = USA
    With  cboCity
    .AddItem "
    Boston"
    .AddItem "
    Miami"
    .AddItem "
    Akron"
    .AddItem "
    Houston”
    End With
    End Select
    End Sub 
    Please tell me which one is the easiest (or most reliable) and what should i change to make it work. (L0 is the row sources of the countries table and L1 is just a sheet with each country with its cities below)

    If u have easier ideas, feel free to tell. Thanks in advance

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: Dependant combo box (country and city case)

    Alternative - load structured table to an array - load the array to the comboboxes - filter the array according to the combobox list column.
    Bad practice to hard code you variables then load them with AddItem - this requires you to add to your code for any additions/deletions.
    Far better just to make alterations in the 'structured table' - self maintains itself.
    Attached Files Attached Files
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,639

    Re: Dependant combo box (country and city case)

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so.

    Please see Forum Rule #3 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important.

    (Note: this requirement is not optional. As you are new here, I will do it for you this time: https://www.mrexcel.com/board/thread...-case.1210030/)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  4. #4
    Registered User
    Join Date
    07-07-2022
    Location
    London
    MS-Off Ver
    2013
    Posts
    16

    Smile Re: Dependant combo box (country and city case)

    I think ur solution works perfectly Torachan, i just need to adjust it to my case (Country instead of vehicle type and City instead of model). Thank you so much.

    P.S : I was looking for this file for another case (to show the price automatically). So double thank you.

  5. #5
    Registered User
    Join Date
    07-07-2022
    Location
    London
    MS-Off Ver
    2013
    Posts
    16

    Re: Dependant combo box (country and city case)

    And Ali, im sorry for cross-posting. I may have missed it when reading the rules. Im so sorry.

  6. #6
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: Dependant combo box (country and city case)

    thanks for the feedback and rep point.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculate distance between two address by Google map (in same city and country)
    By ronlau123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-23-2017, 01:09 AM
  2. [SOLVED] How can I get country name based on city names in the columns
    By alipezu in forum Excel General
    Replies: 10
    Last Post: 11-29-2016, 02:50 AM
  3. City/ State/ Country to TimeZone conversion
    By vij8y in forum Excel General
    Replies: 3
    Last Post: 09-22-2014, 02:21 AM
  4. Data Validation of Country and City
    By csunilkumar in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-05-2014, 08:45 AM
  5. importing city state and country from a ZIPTASTIC website
    By nate02167 in forum Excel General
    Replies: 2
    Last Post: 09-10-2013, 11:06 AM
  6. Formaula to show specific State/City in a Country
    By Sarisha in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-16-2013, 12:01 PM
  7. Replies: 4
    Last Post: 04-04-2013, 03:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1