Help with Cascading ComboBox's

Gregm66

Board Regular
Joined
Jan 23, 2016
Messages
170
Hi everyone,
i am sure that this question has been asked before and i will appologise now if that is so.
i have searched for the answer and had no luck with achieving my objective.
i will attempt to explain this and make it easy to understand, any help with this would be appreciated.
Thanks in Advance

I have a UserForm that holds 3 ComboBox's and several textbox's. My problem is that i am trying to get my third combobox to populate based on the second combobox selection. ComboBox1 ("cboSelect") has a list of 2 items Movies and Music, If Movies is selected then ComboBox2 ("cboCategory") is populated with a list of movie categorys IE: Action, History, Horror etc, or if Music is selected then it displays Rock, Hip Hop, Classical etc these 2 combobox's are functioning perfectly fine. ComboBox3 ("cboName") howerver needs to then populate with only the matching values. So if i select 1.Movies - 2.Action then ComboBox3 ("cboName") would only show all of the movies that are in the action category.

Sheet name is MovieList&Details this has 17 Columns. Column A is the Movie Name, Column B shows if it is Action or Horror etc. This sheet is for Movies Only.
Sheet Named MusicList again has 17 Columns. Column A is the Song Name and Column B is the Genre, Rock, Hip Hop etc.

So cboName has to look at 2 different sheets depending on the selection made in cboCategory and filter the results so that only items matching cboCategory selection are shown.

Hope that is easy to understand.

Thanks Again
 
So is RowSource Music a named range named Music

When I tried that I received the same error you received.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I have not used RowSource in so long I forgot what it was.

See that's why my script had this line of code:
Code:
Private Sub UserForm_Initialize()
cboSelect.AddItem "Movies"
cboSelect.AddItem "Music"
End Sub

So with you setting Rowsource and me using a script to add values to the Combobox this may have caused the error.

So use one or the other but not both.
 
Upvote 0
thanks for your help My Answer Is,

i got it sorted out now cheers.
i have attached the working for you.
Code:
Option Explicit
Dim Dic As Object

Private Sub cboSelect_Change()
Select Case Me.cboSelect
    Case "Movies"
        Me.cboCategory.RowSource = "Movies"
    Case "Music"
        Me.cboCategory.RowSource = "Music"
    Case Else
End Select
End Sub

Private Sub UserForm_Initialize()
Dim Cl As Range
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("MovieList&Details")
      Dic.Add "Movies", CreateObject("scripting.dictionary")
      For Each Cl In .Range("C2", .Range("C" & Rows.Count).End(xlUp))
         If Not Dic("Movies").exists(Cl.Value) Then
            Dic("Movies").Add Cl.Value, CreateObject("scripting.dictionary")
            Dic("Movies")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         ElseIf Not Dic("Movies")(Cl.Value).exists(Cl.Offset(, -1).Value) Then
            Dic("Movies")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         End If
      Next Cl
   End With
   With Sheets("MusicList")
      Dic.Add "Music", CreateObject("scripting.dictionary")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Not Dic("Music").exists(Cl.Value) Then
            Dic("Music").Add Cl.Value, CreateObject("scripting.dictionary")
            Dic("Music")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         ElseIf Not Dic("Music")(Cl.Value).exists(Cl.Offset(, -1).Value) Then
            Dic("Music")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         End If
      Next Cl
   End With
   Me.cboSelect.List = Dic.keys
    With Application
  .WindowState = xlMaximized
  Zoom = Int(.Width / Me.Width * 100)
  Width = .Width
  Height = .Height
  End With
End Sub
Private Sub cboCategory_Click()
   Me.cboName.Clear
   Me.cboName.List = Dic(Me.cboSelect.Value)(Me.cboCategory.Value).keys
End Sub
Again thanks for all your help.
 
Upvote 0
thanks for all your help guys below is the code that is now working.

Code:
Option Explicit
Dim Dic As Object

Private Sub cboSelect_Change()
Select Case Me.cboSelect
    Case "Movies"
        Me.cboCategory.RowSource = "Movies"
    Case "Music"
        Me.cboCategory.RowSource = "Music"
    Case Else
End Select
End Sub

Private Sub UserForm_Initialize()
Dim Cl As Range
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("MovieList&Details")
      Dic.Add "Movies", CreateObject("scripting.dictionary")
      For Each Cl In .Range("C2", .Range("C" & Rows.Count).End(xlUp))
         If Not Dic("Movies").exists(Cl.Value) Then
            Dic("Movies").Add Cl.Value, CreateObject("scripting.dictionary")
            Dic("Movies")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         ElseIf Not Dic("Movies")(Cl.Value).exists(Cl.Offset(, -1).Value) Then
            Dic("Movies")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         End If
      Next Cl
   End With
   With Sheets("MusicList")
      Dic.Add "Music", CreateObject("scripting.dictionary")
      For Each Cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Not Dic("Music").exists(Cl.Value) Then
            Dic("Music").Add Cl.Value, CreateObject("scripting.dictionary")
            Dic("Music")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         ElseIf Not Dic("Music")(Cl.Value).exists(Cl.Offset(, -1).Value) Then
            Dic("Music")(Cl.Value).Add Cl.Offset(, -1).Value, Nothing
         End If
      Next Cl
   End With
   Me.cboSelect.List = Dic.keys
    With Application
  .WindowState = xlMaximized
  Zoom = Int(.Width / Me.Width * 100)
  Width = .Width
  Height = .Height
  End With
End Sub
Private Sub cboCategory_Click()
   Me.cboName.Clear
   Me.cboName.List = Dic(Me.cboSelect.Value)(Me.cboCategory.Value).keys
End Sub

Thanks again for all your help

Regards
Gregm66
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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