dependent Combo boxes on a user form

Nautilus

Board Regular
Joined
Jun 30, 2004
Messages
80
Hello to all!

I'm having trouble creating dependant combo boxes on a user form. In the first combo box user selects type, e.g. DM604 and the following combo box should then list all the possible speed options for that type. As there are a number of different types and each have multiple speed options I'm using .Rowsource = "XXX!TYPES" for the first combo box (TYPES = named range). In my workbook, there is a named range for all the speed options, e.g. there is a named range "DM604" which refers to cells "YYY!Q8:V11" and contains speed options for type DM604. Using data validation method it would be easy but my executives want to use user forms. Can anyone help me to create dependent combo boxes. I've searched for a solution for a couple of hours but could't find one. Thanks in advance!

p.s. I'm quite a newbie when it comes to excel and vba so explicit words of advice would be much appreciated! :-D
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
The main problem is this

I read through the link(s) but I couldn't figure out a solution. The main problem is that the named range of different speed options is not a single row or column (and can not be changed, would propably require too much work). I tried to come up with a code that would read the value in combobox1, then go through each cell in the named range of that particular name and add items one by one in a some kind of loop... Could this be accomplished some how?

And thanks a lot for Your help!

edit:

Private Sub TypeMain_Change()
SpeedMain.RowSource = TypeMain.Value
End Sub

brings the values in first column of the named range to combobox2 but there are more columns than one in the speed options range.
 
Upvote 0
I set up a spreadsheet like this:
BOOK2
ABCDE
1ListItemAItemB
2ItemAItemA1ItemA4ItemB1ItemB4
3ItemBItemA2ItemA5ItemB2ItemB5
4ItemA3ItemA6ItemB3ItemB6
5
6List=Sheet1!$A$2:$A$3
7ItemA=Sheet1!$B$2:$C$4
8ItemB=Sheet1!$D$2:$E$4
Sheet1


with the named ranges as shown.

Then I used this code:

Code:
Private Sub ComboBox1_Change()
    Dim Rng As Range
    Dim r As Integer
    Dim c As Integer
    Set Rng = Range(ComboBox1.Value)
    ComboBox2.Clear
    For c = 1 To Rng.Columns.Count
        For r = 1 To Rng.Rows.Count
            ComboBox2.AddItem Rng.Cells(r, c)
        Next r
    Next c
End Sub

Hope that helps.
 
Upvote 0
Thanks Andrew,

That worked out great!

I would like to ask one more thing in order to fine tune the user interface.
Values in the named range are set up like this (I propably should have posted this right away)

DM604 G5 7,5 9,5 14 16 2
DM604 G6 7,5 9,5 19 16 30
DM604 G7 9,5 22 30
DM604 G8 24 36

(hope that this shows up so that it makes sense, btw G"X" is the class and the named range contains values from 7,5 to 36, not the type or the class of course...)

So, there are blank cells and cells that contain the same number. Is there any way that the procedure would add only unique number values (no blanks), preferably in ascending order? Or is it too difficult? Anyway, thanks again for your time and effort, :-D cheers!
 
Upvote 0
It came to my mind that sort method could be used to variable Rng, so that the original values would not get mixed up, but the question is how :cry: I think it should be something like this

Rng.Sort(xxx, xlAscending)

but where should I put it in following code

Code:
Private Sub TypeMain_Change()
On Error GoTo Ex
    Dim Rng As Range
    Dim r As Integer
    Dim c As Integer
    Set Rng = Range(TypeMain.Value)
    SpeedMain.Clear
    For c = 1 To Rng.Columns.Count
        For r = 1 To Rng.Rows.Count
            SpeedMain.AddItem Rng.Cells(r, c)
        Next r
    Next c
    
Ex:
With SpeedMain
.RowSource = ""
.Text = ""
End With

End Sub

And once again, thanks for the help (in advance)!
 
Upvote 0
Sorry to post again on the same subject, couldn't use edit any more...

Well, I tried to get the sort method work but I'm afraid that the range must be copied to a temporary sheet if it must be sorted, any pointers on how this should be done? And how should the code for sort be written when data that must be sorted is in this type of format:
Code:
4,5	7,1	7,5	12		  24
4,5	9	  7,5	15	17   30
4,5	11		    15	19   30
	   12		    17	24   34

edit: problem solved, thanks to Andrew for Helping! :-D
 
Upvote 0
With this spreadsheet:
ComboBoxSynchUnique.xls
ABCDEFG
1ListItemA7.59.514162
2ItemAItemA7.59.5191630
3ItemBItemA9.52230
4ItemA2436
5ItemB12.514.519217
6ItemB12.514.5242135
7ItemB14.52735
8ItemB2941
9
10List=Sheet1!$A$2:$A$3
11ItemA=Sheet1!$C$1:$G$4
12ItemB=Sheet1!$C$5:$G$8
Sheet1


This code will create in ComboBox2 a list of unique items from the range selected in ComboBox1:

Code:
Private Sub ComboBox1_Change()
    Dim Rng As Range
    Dim c As Range
    Dim TheList As New Collection
    Dim i As Long
    Dim j As Long
    Dim Temp As Variant
    Dim Item
    Set Rng = Range(ComboBox1.Value)
    ComboBox2.Clear
'   Create unique list
    On Error Resume Next
    For Each c In Rng
        TheList.Add c.Value, CStr(c.Value)
    Next c
    On Error GoTo 0
'   Sort the list
    If TheList.Count > 1 Then
        For i = 1 To TheList.Count - 1
            For j = i + 1 To TheList.Count
                If TheList(i) > TheList(j) Then
                    Temp = TheList(j)
                    TheList.Remove (j)
                    TheList.Add Temp, CStr(Temp), i
                End If
            Next j
        Next i
    End If
    For Each Item In TheList
        ComboBox2.AddItem Item
    Next Item
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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