I have three drop downs in my VBA form (cbo_fac1, cbo_fac2, cbo_fac3), each extracting data from the same source. But I would like to implement a cascade update on the group of select lists so that when the user picks an option from one it is removed from subsequent select lists.
For example if cbo_fac1 has the following options:
Blu-ray DVD Player
Chalk board
Computer
Data projector
Data projector trolley
and the user selects Blu-ray DVD Player from cbo_fac1 then the next two drop downs (cbo_fac2 and cbo_fac3) should only have the following options available:
Chalk board
Computer
Data projector
Data projector trolley
If the user then decides to pick Data projector trolley from cbo_fac2 then the next and final drop (cbo_fac3) down should only have the following options for selection:
Chalk board
Computer
Data projector
Of course if the user decides to go back and change their options then this should also reflect. How would I go about achieving this. This is the code I have so far:
For Each c_fac In ws_misc.Range("fac")
With Me.cbo_fac1
.AddItem c_fac.Value
.List(.ListCount - 1, 1) = c_fac.Offset(0, 1).Value
End With
With Me.cbo_fac2
.AddItem c_fac.Value
.List(.ListCount - 1, 1) = c_fac.Offset(0, 1).Value
End With
With Me.cbo_fac3
.AddItem c_fac.Value
.List(.ListCount - 1, 1) = c_fac.Offset(0, 1).Value
End With
Next c_fac
Thanks in advance!
This took longer than I thought. I thought it was going to be easier 🙂
I would use a User Defined Type in VBA for this solution. Please see this example:
Put this in a Module:
Add three combo boxes on a userform. Change the combo boxes to the names: cbo_fac1, cbo_fac2, cbo_fac3.
Then add this code behind the userform:
The idea here is that after each combobox has been selected a value, that it would reset the other comboboxes using the AferUpdate event. It also takes into account if a combobox already had a value selected.
Hope this helps
EDIT:
I changed the code to accommodate data in a worksheet. I named the sheet “Sheet1” (change this to whatever you need) and I’m assuming that in that worksheet that the ONLY data in there is the list of items you want to have listed (so, NO headers and NO other data in the worksheet at all).