Excel VBA Dependent Dropdown Boxes

preview_player
Показать описание
Here you find the code from the video, to open the editor

Private Sub ComboBox1_DropButtonClick() 'Be sure that it says "_DropButtonClick()" after the ComboBox name, in this case it's ComboBox1_DropButtonClick()
Me.ComboBox1.AddItem "First" 'This is Case 0
Me.ComboBox1.AddItem "Second" 'This is Case 1
Me.ComboBox1.AddItem "Third" 'This is Case 2
'Me.ComboBox1.AddItem "Fourth" 'This is Case 3 'Feel free to add as many options as you need like so.
End Sub
Private Sub ComboBox2_DropButtonClick() 'Be sure that it says "_DropButtonClick()" after the ComboBox name, in this case it's ComboBox2_DropButtonClick()
Dim index As Integer
index = ComboBox1.ListIndex 'This is determining the selection from the previous ComboBox, make sure the names are the same such as ComboBox1 in this case.
Select Case index
Case Is = 0 'This is where it calls the Case and gives you the new options
With ComboBox2
.AddItem "First A"
.AddItem "First B"
.AddItem "First C"
.AddItem "First D"
End With
Case Is = 1 'This is where it calls the Case and gives you the new options
With ComboBox2
.AddItem "Second A"
.AddItem "Second B"
.AddItem "Second C"
.AddItem "Second D"
End With
Case Is = 2 'This is where it calls the Case and gives you the new options
With ComboBox2
.AddItem "Third A"
.AddItem "Third B"
.AddItem "Third C"
.AddItem "Third D"
End With
'Case Is = 3 'Add more cases by copying and pasting what you see below and editing it to match as above.
'With ComboBox2
'.AddItem "Fourth A"
'.AddItem "Fourth B"
'.AddItem "Fourth C"
'.AddItem "Fourth D"
'End With
End Select
End Sub

Private Sub CommandButton1_Click() 'This is a simple reset by unloading the userform and loading it again. Otherwise the dropdowns duplicate themselves.
Unload Me
UserForm1.Show
End Sub

Excel VBA, Dropdown, Cascading, Dependent
Рекомендации по теме