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

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
26
Office Version
  1. 365
  2. 2013
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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
What values do you want to add to the other combo?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Solution
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 ?
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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