VBA Code - More than one Depended Combobox.

RAKESH230583

New Member
Joined
Jan 10, 2011
Messages
46
Hi Friends,

Case : Below table shows some product details, where in under column A we have only 2 product i.e. Product A & Product B.
Column B represent product Group & Last column C represent Grade of (Product and its group).

Excel Sheet 1.

ABC
1ProductGroupGrade
2Product AGoldGrade I
3Product AGoldGrade II
4Product ASilverGrade I
5Product ASilverGrade II
6Product ASilverGrade III
7Product APlatinumGrade I
8Product BGoldGrade I
9Product BGoldGrade II
10Product BGoldGrade III
11Product BGoldGrade IV
12Product BSilverGrade I

<tbody>
</tbody>

I had created 3 ComboBoxes in Excel userform - But i am not able to get the desired drop down list. As, all three are inter related to each other.

Requirement Example:
Combobox1 : User must see only 2 Product (Product A & B)
Combobox2 : User must see only those Group which are available against that product (i.e Platinum must show only against Product A and not against Product B) - Post selecting product in Combobox1
Combobox3 : User must see only those Grade which are available against that product & Group (i.e. if user select product as "A" and group as "Platinum", then under combobox3 - User must see only 1 Grade i.e. "Grade I")
.
I hope i am clear with above example -

Kindly share your knowledge, for me to understand how to tackle above with vba code.

Thanks in advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about
Code:
Option Explicit
Private Dic As Object

Private Sub ComboBox1_AfterUpdate()
   ComboBox2.Clear
   ComboBox2.List = Dic(ComboBox1.Value).keys
End Sub

Private Sub ComboBox2_AfterUpdate()
   ComboBox3.Clear
   ComboBox3.List = Dic(ComboBox1.Value)(ComboBox2.Value).keys
End Sub

Private Sub UserForm_Initialize()
   
   Dim v1 As String, v2 As String, v3 As String
   Dim Cl As Range
   
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = vbTextCompare
   
   With Sheets("[COLOR=#ff0000]pcode[/COLOR]")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         v1 = Cl.Value: v2 = Cl.Offset(, 1).Value: v3 = Cl.Offset(, 2).Value
         If Not Dic.exists(v1) Then
            Dic.Add v1, CreateObject("scripting.dictionary")
            Dic(v1).Add v2, CreateObject("scripting.dictionary")
            Dic(v1)(v2).Add v3, Nothing
         ElseIf Not Dic(v1).exists(v2) Then
            Dic(v1).Add v2, CreateObject("scripting.dictionary")
            Dic(v1)(v2).Add v3, Nothing
         ElseIf Not Dic(v1)(v2).exists(v3) Then
            Dic(v1)(v2).Add v3, Nothing
         End If
      Next Cl
   End With
   ComboBox1.List = Dic.keys
End Sub
Change sheet name in red to suit
 
Upvote 0
Hi Thanks,

With above given Codes i am able to view the details in combobox1, But not in Combobox2 and combobox3
Instead of Private Sub ComboBox2_AfterUpdate() - can we use Change() ?
 
Last edited:
Upvote 0
You can use Click rather than AfterUpdate
Code:
Private Sub ComboBox1_click()
   ComboBox2.Clear
   ComboBox2.List = Dic(ComboBox1.Value).keys
End Sub
Private Sub ComboBox2_click()
   ComboBox3.Clear
   ComboBox3.List = Dic(ComboBox1.Value)(ComboBox2.Value).keys
End Sub
but Change is a lot more complicated.
Are your comboboxs called Combobox2 & Combobox3?
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,666
Members
448,977
Latest member
moonlight6

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