excel userform / combobox with dependent list

Hi experts,

I have a combo box on a userform that the user can choose a colour, e.g.  red, green, blue etc. how do i create a dependent list so that when colour red is selected, the number 4 writes to sheet 1 instead of the colour?.  THe users do not know the codes/assigned number but do know the colour. Can a dependent list be created on a userform? If so, how and how can i force the code to write the number rather than the colour?

This is a very simplified dummy example as I can't post the spreadsheet due to confidentiality.

My list is as follows:
Colours / Code
Blue              1
Orange      2
Green      3
Red              4

thanks heaps!
Who is Participating?
Rory ArchibaldConnect With a Mentor Commented:
Just use a two column list and write the second column back to the sheet (you can hide it in the combobox by setting its column width to 0) using:
Cells(1, 1).value = me.combobox1.list(me.combobox1.listindex, 1)
for example.
Post a workbook with dummy data. How are you populating your combo box list? If you have a list in the spreadsheet you could use VLOOKUP or similar.
Saurabh Singh TeotiaCommented:
Assuming the listbox is a multiple listbox then you can give the following code in the listbox change event...
Also i have assumed that you know the series order of the listbox and it as per shown here in your example...This will give value in the range a1 of your sheet.

dim iItem as long

  For iItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(iItem) = True Then
exit sub
end if
Next iItem

Open in new window

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Saurabh Singh TeotiaCommented:
Oops forget my code i didnt check that you are working on combox i thought you are working on listbox...
StephenJRConnect With a Mentor Commented:
Perhaps like this if you have defined the row source for the combo box.

Private Sub ComboBox1_Change()

Range("A1").Value = Application.Match(Me.ComboBox1.Value, Range(Me.ComboBox1.RowSource), 0)

End Sub
JKCCAuthor Commented:
ok, I will try these out . thanks
JKCCAuthor Commented:
thanks again rorya and stephenJR. I implemented it and it worked! I did have the rowsource defined.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.