Question

I have an excel File with three columns. Each row is unique. There are three comboboxes corresponding to those three columns.

When an option in the first combobox is selected, I want the second combobox to be populated with items from the second column corresponding to the selection from the first combobox. ie:

- Adam | Apple | Seed

- Adam | Apple | Core

- Adam | Orange | Peel

- Jess | Banana | Peel

- Jess | Mango | Pulp

- Jess | Orange | Seed

Here the first column has data Adam, Jess. The first combobox already has two unique options Adam and Jess.

Once the user has selected either Adam or Jess the second combobox should be populated with corresponding fruit names.

Ex. Combobox1 = Adam

then combobox2 ={Apple, Orange}

when either Apple / Orange are selected then the third combobox should have options correspoding to the first two comboboxes.

Please note that my first combobox is already populated with data correctly:

Function UniqueList()
'Populate control with
'unique list.

Range("Names").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Range("uniqueNames"), Unique:=True

'Set combo control's Row Source property.

Range("uniqueNames").RemoveDuplicates Columns:=1, Header:=xlNo
UserForm1.uniqueNameList.RowSource = Selection.CurrentRegion.Address

'Display user form.
UserForm1.Show

Selection.CurrentRegion.Clear

End Function

EDIT: I basically need to say something like this: populate second combobox with cells of the second row where the first row is equal to combobox1.

Was it helpful?

Solution

Lets say you have your data in sheet1:

enter image description here

1- You can use combobox_Change event handlers to track when the user has selected a new value from a combobox.

2- Combobox.listindex: This will give you the currently selected index in the combobox

3- Combobox.list: Is an array with all the items in the combo box

 Private Sub ComboBox1_Change()
    Dim strValue As String
    Dim i As Integer
    If ComboBox1.ListIndex <> -1 Then
        strValue = ComboBox1.List(ComboBox1.ListIndex)
        ComboBox2.Clear
    ComboBox3.Clear
    For i = 1 To 6
        If Cells(i, 1) = strValue Then
            If exists(ComboBox2, Cells(i, 2)) = False Then
                ComboBox2.AddItem (Cells(i, 2))
            End If
            If exists(ComboBox3, Cells(i, 3)) = False Then
                ComboBox3.AddItem (Cells(i, 3))
            End If

        End If

    Next i
Else
    ComboBox2.Clear
    ComboBox3.Clear
End If

End Sub



Private Sub ComboBox2_Change()
Dim strValue1 As String
Dim strValue2 As String
Dim i As Integer
If (ComboBox2.ListIndex <> -1) And (ComboBox1.ListIndex <> -1) Then
    strValue1 = ComboBox2.List(ComboBox2.ListIndex)
    strValue2 = ComboBox1.List(ComboBox1.ListIndex)
    ComboBox3.Clear
    For i = 1 To 6
        If (Cells(i, 2) = strValue1) And (Cells(i, 1) = strValue2) Then
            If exists(ComboBox3, Cells(i, 3)) = False Then
                ComboBox3.AddItem (Cells(i, 3))
            End If    
        End If
    Next i
    Else
        ComboBox3.Clear  
    End If
End Sub

Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 6
    If exists(ComboBox1, Cells(i, 1)) = False Then
        ComboBox1.AddItem (Cells(i, 1))
    End If
Next i
End Sub

Private Function exists(ByRef objCmb As ComboBox, ByVal strValue As String) As Boolean
Dim i  As Integer

For i = 1 To objCmb.ListCount
    If strValue = objCmb.List(i - 1) Then
        exists = True
        Exit Function
    End If

Next i
exists = False
End Function

The code will work assuming you have 3 combo boxes with the names Combobox1, Combobox2 and Combobox3 (the default names the VBA editor creates)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top