I'm trying to setup 3 ComboBoxes in an userform.
- ComboBox1 = Yes/No/Blank
ComboBox2 = A List of Options
- Option1
- Option2
- Option3
- Option4
- Option5
- Option6
- Option7
- Option8
ComboBox3 = Is where I'm having the issue and I need help with.
ComboBox3 is dependant of a combination of ComboBox1 and ComboBox2
I hope I can explain this well.
For instance.
If ComboBox1 = "" Then do not display any options in ComboBox2 and Combobox3
If ComboBox1 = Yes and ComboBox2 = Option1 Then Give me this list of options
If ComboBox1 = No and ComboBox2 = Option1 Then Give me this other list of options.
If ComboBox1 = Yes and ComboBox2 = Option2 Then Give me this list of options
If ComboBox1 = No and ComboBox2 = Option2 Then Give me this other list of options.
And so on for every option.
Hope that makes sense and someone can shed some light on this.
Let me know if more details are needed.
Thanks.
UPDATE
After Researching I came across a script that does the job but I'm having issues implementing it to my current code. I tested it on a separate form and it all works as intended. How I can integrate this to my current UserForm_Initialize()?
I placed this part at the beginning of the form before the UserForm_Initialize() Sub. (I also tried adding it after)
Private dic As Object
Private Sub ComboBox1_Change()
With Me
.ComboBox2.Clear
.ComboBox3.Clear
If .ComboBox1.ListIndex <> -1 Then
.ComboBox2.List = dic(.ComboBox1.Value).keys
End If
End With
End Sub
Private Sub ComboBox2_Change()
With Me
.ComboBox3.Clear
If .ComboBox2.ListIndex <> -1 Then
.ComboBox3.List = dic(.ComboBox1.Value)(.ComboBox2.Value).keys
End If
End With
End Sub
This is the second part that goes into the UserForm_Initialize() sub and I keep getting an error "Run-Time Error 9. Subscript out of range". The worksheet "Lists" exists and it has all of the necessary information. If I try this separately on another form, it works.
Private Sub UserForm_Initialize()
Dim a, i As Long
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = 1
a = Sheets("Lists").Cells(1).CurrentRegion.Value
For i = 2 To UBound(a, 1)
If Not dic.exists(a(i, 2)) Then
Set dic(a(i, 2)) = CreateObject("Scripting.Dictionary")
End If
If Not dic(a(i, 2)).exists(a(i, 1)) Then
Set dic(a(i, 2))(a(i, 1)) = CreateObject("Scripting.Dictionary")
End If
dic(a(i, 2))(a(i, 1))(a(i, 3)) = i
Next
Me.ComboBox1.List = dic.keys
End Sub
This is what my UserForm_Initialize() originally looks like without this code. This is for me to load some other comboboxes.
Private Sub UserForm_Initialize()
Dim rngResponse As Range
Dim ws As Worksheet
Set ws = Worksheets("Lists")
For Each rngResponse In ws.Range("Response")
Me.cbRes1.AddItem rngResponse.Value
Me.cbRes2.AddItem rngResponse.Value
Me.cbRes3.AddItem rngResponse.Value
Me.cbRes4.AddItem rngResponse.Value
Me.cbRes5.AddItem rngResponse.Value
Me.cbRes1.AddItem rngResponse.Value
Next rngResponse
End Sub
ComboBox_Changeevent on the userform to program this. First do it for ComboBox1 so that if it is blank, blank out the other two and Clear ComboBox 3's list. Then have a change event on ComboBox2 to adjust ComboBox3's options based on what has been selected in ComboBoxes 1 & 2. If you run into specific problems, update your question with the code you've come up with and where you're having issues. - tigeravatarUserForm_Initializecode, it shows five comboboxes. Are we working with three or five comboboxes? - tigeravatar