Dependant combo box (country and city case)

Ronald45

New Member
Joined
Jul 7, 2022
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
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.

VBA Code:
Dim colonne As Integer
Dim i As Integer, j As Integer

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

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


VBA Code:
Dim colonne As Integer
Dim i As Integer, j As Integer

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

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


VBA 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 :)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Dependant combo box (country and city case)
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Not sure what you found, but please do not mark a post as the solution when it does not contain one.
 
Upvote 0
Im sorry, im not used to the forum rules (eventhough i read the rules). I'll make sure it wont happen again.

Here's the solution i found. Thanks.
 
Upvote 0
VBA Code:
Private OL As Worksheet
Private TS As ListObject

Private Sub UserForm_Initialize()
Set OL = Worksheets("Sheet")
Set TS = OL.ListObjects(1)
Me.cboCountry.List = Application.Transpose(TS.HeaderRowRange.Value)
End Sub

Private Sub cboCountry_Change()
Dim COL As Byte

Me.cboCity.Clear
COL = Me.cboCountry.ListIndex + 1
For I = 1 To TS.ListRows.Count
    If TS.DataBodyRange(, COL) <> "" Then Me.cboCity.AddItem TS.DataBodyRange(I, COL)
Next I
End Sub

With "sheet" being the worsheet i worked on and which contained the countries and cities as below.

1657912885853.png
 

Attachments

  • 1657912910347.png
    1657912910347.png
    13.1 KB · Views: 0
Upvote 0
Solution

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top