I need to populate two combo boxes from excel sheet, the data will be like below:
Column | A Column B ---------------------- A | 1 A | 2 A | 3 A | 3 A | 5 B |10 B | 11 B | 12 A | 1 A | 5 A | 2
So from the above data, one combo box should hold unique values A & B.
On selecting a value from the 1st combo box A or B, respective values should be populated in 2nd combo box.
So the data should be like below:
If A is selected in the 1st combo box, then 2nd combo box should only show the values 1,2,3,4 & 5. If B is selected in the 1st combo box, then 2nd combo box should only show the values 10,11 & 12.
for that i have following code : -
Private Sub ComboBox1_Change()
Dim rng As Range
Set rng = Sheet2.Range("B2", Sheet2.Cells(Rows.Count, "b").End(xlUp))
Set oDictionary = CreateObject("Scripting.Dictionary")
Sheet1.ComboBox2.Clear
With Sheet1.ComboBox2
For Each cel In rng
If ComboBox1.Value = cel.Offset(, -1).Value Then
oDictionary(cel.Value) = 0
.AddItem (cel.Value)
End If
Next cel
End With
End Sub
Private Sub ComboBox1_DropButtonClick()
Dim rng As Range
Set rng = Sheet2.Range("A2", Sheet2.Cells(Rows.Count, "A").End(xlUp))
Set oDictionary = CreateObject("Scripting.Dictionary") 'to put uniqe values from rng variable to combo box1
With oDictionary
For Each cel In rng
If Not .exists(cel.Value) Then
.Add cel.Value, Nothing
End If
Next cel
Sheet1.ComboBox1.List = .keys
End With
End Sub
problem is that it combobox does not shows unique values .
how i can get unique values in combobox2 .
you can ignore my coding and provide simplest way to do above said task ...