How do I add an independent combobox in a dependent combobox vba code?

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
I have created a userform with 3 dependent comboboxes and it works well. Can anyone please tell me how I can add another combobox (with items) within the same code, but is independent from the rest of the comboboxes. Here's the code I used. Thank you @DanteAmor for posting the code somewhere else. :) You spared me a lot of stress. :LOL:

Excel Formula:
Option Explicit
Dim sh As Worksheet

Private Sub ComboBox1_Change()
  Dim c As Range, dic As Object
  Set dic = CreateObject("Scripting.Dictionary")
  
  ComboBox2.Clear
  ComboBox3.Clear
  
  For Each c In sh.Range("A2", sh.Range("A" & Rows.Count).End(xlUp))
    If c.Value = ComboBox1.Value Then
      dic(c.Offset(0, 1).Value) = Empty
    End If
  Next
  ComboBox2.List = dic.keys
End Sub

Private Sub ComboBox2_Change()
  Dim c As Range, dic As Object
  ComboBox3.Clear
  
  For Each c In sh.Range("A2", sh.Range("A" & Rows.Count).End(xlUp))
    If c.Value = ComboBox1.Value And c.Offset(0, 1).Value = ComboBox2.Value Then
      ComboBox3.AddItem c.Offset(, 2).Value
    End If
  Next
End Sub



Private Sub UserForm_Activate()
  Dim c As Range, dic As Object
  Set sh = Sheets("Database")
  Set dic = CreateObject("Scripting.Dictionary")
  For Each c In sh.Range("A2", sh.Range("A" & Rows.Count).End(xlUp))
    dic(c.Value) = Empty
  Next
  ComboBox1.List = dic.keys
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,162
Office Version
  1. 365
Platform
  1. Windows
What values do you want to add to the other combo?
 

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
What values do you want to add to the other combo?
Just anything. Like "Cat 1" "Cat 2" etc. I did do the addlisting thing, but it doesn't show up on the box. I don't know what I did wrong there.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,162
Office Version
  1. 365
Platform
  1. Windows
Ok, you can just put something like this in the Userform activate event
VBA Code:
With ComboBox4
    .AddItem "MAIN 1"
    .AddItem "MAIN 2"
    .AddItem "MAIN 3"
    .AddItem "MAIN 4"
    .AddItem "MAIN 5"
    .AddItem "MAIN 6"
    .AddItem "MAIN 7"
    .AddItem "MAIN 8"
End With
 
Solution

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Ok, you can just put something like this in the Userform activate event
VBA Code:
With ComboBox4
    .AddItem "MAIN 1"
    .AddItem "MAIN 2"
    .AddItem "MAIN 3"
    .AddItem "MAIN 4"
    .AddItem "MAIN 5"
    .AddItem "MAIN 6"
    .AddItem "MAIN 7"
    .AddItem "MAIN 8"
End With
Thank you very much Fluff. That works 😁
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,162
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,140,929
Messages
5,703,225
Members
421,285
Latest member
Bebek

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
Top