change code populate items for four comboboxes instead of three for each row on userfrom

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
this code works for each three comboboxes dapends on each othere of them for each row on userform . waht I want to change into four comboboxes for each row on userform
here is the code
VBA Code:
Option Explicit
Private dic As Object
Dim a, i As Long, ii As Long

Private Sub UserForm_Initialize()
Set dic = CreateObject("Scripting.Dictionary")
    a = Sheets("inventory").Cells(1).CurrentRegion.Value
    For i = 2 To UBound(a, 1)
        For ii = 1 To UBound(a, 2)
            a(i, ii) = a(i, ii) & ""
        Next
        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, 3)) Then
      
            Set dic(a(i, 2))(a(i, 3)) = CreateObject("Scripting.Dictionary")
        End If
        dic(a(i, 2))(a(i, 3))(a(i, 4)) = Empty
    Next
    a = mySort(dic.keys)
    Me.ComboBox1.List = a
Me.ComboBox4.List = a   '**********add this line
Me.ComboBox7.List = a   '**********add this line
Me.ComboBox10.List = a '**********add this line
End Sub

Private Sub ComboBox1_Change()
Me.ComboBox2.Clear: Me.ComboBox3.Clear
    If Me.ComboBox1.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox1.Value).keys)
    Me.ComboBox2.List = a
End Sub

Private Sub ComboBox4_Change()  '*************add this sub
Me.ComboBox5.Clear: Me.ComboBox6.Clear
    If Me.ComboBox4.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox4.Value).keys)
    Me.ComboBox5.List = a
End Sub

Private Sub ComboBox7_Change()  '************add this sub
    Me.ComboBox8.Clear: Me.ComboBox9.Clear
    If Me.ComboBox7.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox7.Value).keys)
    Me.ComboBox8.List = a
End Sub

Private Sub ComboBox10_Change()   '***********add this sub
    Me.ComboBox11.Clear: Me.ComboBox12.Clear
    If Me.ComboBox10.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox10.Value).keys)
    Me.ComboBox11.List = a
End Sub

Private Sub ComboBox2_Change()
    Me.ComboBox3.Clear
    If Me.ComboBox2.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox1.Value)(Me.ComboBox2.Value).keys)
    Me.ComboBox3.List = a
End Sub

Private Sub ComboBox5_Change()    '***********add this sub
    Me.ComboBox6.Clear
    If Me.ComboBox5.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox4.Value)(Me.ComboBox5.Value).keys)
    Me.ComboBox6.List = a
End Sub

Private Sub ComboBox8_Change()     '***********add this sub
    Me.ComboBox9.Clear
    If Me.ComboBox8.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox7.Value)(Me.ComboBox8.Value).keys)
    Me.ComboBox9.List = a
End Sub

Private Sub ComboBox11_Change()    '***********add this sub
    Me.ComboBox12.Clear
    If Me.ComboBox11.ListIndex = -1 Then Exit Sub
    a = mySort(dic(Me.ComboBox10.Value)(Me.ComboBox11.Value).keys)
    Me.ComboBox12.List = a
End Sub

Function mySort(a)
    Dim i As Long, ii As Long, temp
    For i = LBound(a) To UBound(a) - 1
        For ii = i + 1 To UBound(a)
            If a(i) > a(ii) Then
                temp = a(i): a(i) = a(ii): a(ii) = temp
            End If
        Next
    Next
    mySort = a
End Function
any help please ?

11.PNG


Option Explicit
Private dic As Object
Dim a, i As Long, ii As Long

Private Sub UserForm_Initialize()
Set dic = CreateObject("Scripting.Dictionary")
a = Sheets("inventory").Cells(1).CurrentRegion.Value
For i = 2 To UBound(a, 1)
For ii = 1 To UBound(a, 2)
a(i, ii) = a(i, ii) & ""
Next
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, 3)) Then
Set dic(a(i, 2))(a(i, 3)) = CreateObject("Scripting.Dictionary")
End If
dic(a(i, 2))(a(i, 3))(a(i, 4)) = Empty
Next
a = mySort(dic.keys)
Me.ComboBox1.List = a
Me.ComboBox4.List = a '**********add this line
Me.ComboBox7.List = a '**********add this line
Me.ComboBox10.List = a '**********add this line
End Sub

Private Sub ComboBox1_Change()
Me.ComboBox2.Clear: Me.ComboBox3.Clear
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox1.Value).keys)
Me.ComboBox2.List = a
End Sub

Private Sub ComboBox4_Change() '*************add this sub
Me.ComboBox5.Clear: Me.ComboBox6.Clear
If Me.ComboBox4.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox4.Value).keys)
Me.ComboBox5.List = a
End Sub

Private Sub ComboBox7_Change() '************add this sub
Me.ComboBox8.Clear: Me.ComboBox9.Clear
If Me.ComboBox7.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox7.Value).keys)
Me.ComboBox8.List = a
End Sub

Private Sub ComboBox10_Change() '***********add this sub
Me.ComboBox11.Clear: Me.ComboBox12.Clear
If Me.ComboBox10.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox10.Value).keys)
Me.ComboBox11.List = a
End Sub

Private Sub ComboBox2_Change()
Me.ComboBox3.Clear
If Me.ComboBox2.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox1.Value)(Me.ComboBox2.Value).keys)
Me.ComboBox3.List = a
End Sub

Private Sub ComboBox5_Change() '***********add this sub
Me.ComboBox6.Clear
If Me.ComboBox5.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox4.Value)(Me.ComboBox5.Value).keys)
Me.ComboBox6.List = a
End Sub

Private Sub ComboBox8_Change() '***********add this sub
Me.ComboBox9.Clear
If Me.ComboBox8.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox7.Value)(Me.ComboBox8.Value).keys)
Me.ComboBox9.List = a
End Sub

Private Sub ComboBox11_Change() '***********add this sub
Me.ComboBox12.Clear
If Me.ComboBox11.ListIndex = -1 Then Exit Sub
a = mySort(dic(Me.ComboBox10.Value)(Me.ComboBox11.Value).keys)
Me.ComboBox12.List = a
End Sub

Function mySort(a)
Dim i As Long, ii As Long, temp
For i = LBound(a) To UBound(a) - 1
For ii = i + 1 To UBound(a)
If a(i) > a(ii) Then
temp = a(i): a(i) = a(ii): a(ii) = temp
End If
Next
Next
mySort = a
End Function
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top