Three dependent ComboBoxes

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
Through a UserForm, I perform data searches in a given Excel sheet. This UserForm, among others, has three ComboBoxes:
ComboBox1, select countries.
ComboBox2, dependent on ComboBox1, selects cities belonging to the country we have chosen in ComboBox1.
ComboBox3, selects names.

CODES:

Rich (BB code):
'
Dim dar As Object
Dim va, vb, vc
Dim n As Integer
'
Private Sub ComboBox1_Enter()
    Dim x
    dar.Clear
    For Each x In va
        If Not dar.Contains(x) Then dar.Add CStr(x)
    Next
    dar.Sort
    ComboBox1.List = dar.toArray()
End Sub
'
Private Sub ComboBox2_Enter()
    Dim i As Long, tx As String
    dar.Clear:    tx = UCase(ComboBox1)
    For i = LBound(va) To UBound(va)
        If UCase(va(i, 1)) = tx Then
            If Not dar.Contains(vb(i, 1)) Then dar.Add vb(i, 1)
        End If
    Next
    dar.Sort
    ComboBox2.List = dar.toArray()
End Sub

'
Private Sub ComboBox3_Enter()
    Dim x
    dar.Clear
    For Each x In vc
        If Not dar.Contains(x) Then dar.Add CStr(x)
    Next
    dar.Sort
    ComboBox3.List = dar.toArray()
End Sub

'
Private Sub UserForm_Initialize()
    With Sheets("BDATOS")
        n = .Range("B" & Rows.Count).End(xlUp).Row
        va = .Range("G2:G" & n) 'ComboBox1 = By country of destination
        vb = .Range("H2:H" & n) 'ComboBox2 = By destination city
        vc = .Range("B2:B" & n) 'ComboBox3 = By name
    End With
    Set dar = CreateObject("System.Collections.Arraylist")
End Sub
'

I need the ComboBox3 to be dependent on ComboBox1 and ComboBox2; but I can't find the code that allows it.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
@Aretradeser
Replace "Private Sub ComboBox3_Enter" with this:
VBA Code:
Private Sub ComboBox3_Enter()
    Dim x, tx1 As String, tx2 As String, i As Long
    dar.Clear
    tx1 = UCase(ComboBox1)
    tx2 = UCase(ComboBox2)
    For i = LBound(vc) To UBound(vc)
        If UCase(va(i, 1)) = tx1 And UCase(vb(i, 1)) = tx2 Then
            If Not dar.Contains(vc(i, 1)) Then dar.Add vc(i, 1)
        End If
    Next
    If dar.Count > 0 Then
        dar.Sort
        ComboBox3.List = dar.toArray()
    End If
End Sub

Also add this:
VBA Code:
Private Sub ComboBox1_Change()
ComboBox2.Value = ""
ComboBox3.Value = ""
End Sub

Private Sub ComboBox2_Change()
ComboBox3.Value = ""
End Sub
 
Upvote 0
Solution
@Aretradeser
Replace "Private Sub ComboBox3_Enter" with this:
VBA Code:
Private Sub ComboBox3_Enter()
    Dim x, tx1 As String, tx2 As String, i As Long
    dar.Clear
    tx1 = UCase(ComboBox1)
    tx2 = UCase(ComboBox2)
    For i = LBound(vc) To UBound(vc)
        If UCase(va(i, 1)) = tx1 And UCase(vb(i, 1)) = tx2 Then
            If Not dar.Contains(vc(i, 1)) Then dar.Add vc(i, 1)
        End If
    Next
    If dar.Count > 0 Then
        dar.Sort
        ComboBox3.List = dar.toArray()
    End If
End Sub

Also add this:
VBA Code:
Private Sub ComboBox1_Change()
ComboBox2.Value = ""
ComboBox3.Value = ""
End Sub

Private Sub ComboBox2_Change()
ComboBox3.Value = ""
End Sub
Akuini, a pleasure to have your invaluable help. Your code works perfectly.
Thank you
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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