I have a combo box (in a user form in Excel) whose data source is set of menu items with headers (a named range in a worksheet). Right now, my workaround is to erase these headers (e.g. Main Course, Desserts, Beverages, etc.) but I would still like to know if it's possible to add these unselectable headers so that the end user has distinctions between the different menu items. Any help would be very much appreciated :)
e.g.
Main Courses (**unselectable**)
Roast Beef with Mashed Potato (selectable)
Spicy Spareribs (selectable)
Beef Stroganoff (selectable)
Roast Chicken (selectable)
Desserts (**unselectable**)
Mango Float (selectable)
Brownies (selectable)
Lemon Squares (selectable)
No AFAIK, you can't do that. Below are two alternatives. Take your pick :)
Alternative 1
Deselect the moment user selects the relevant header. For example
Private Sub UserForm_Initialize()
ComboBox1.Style = fmStyleDropDownList
ComboBox1.AddItem "--- Main Courses ---"
ComboBox1.AddItem "Roast Beef with Mashed Potato"
ComboBox1.AddItem "Spicy Spareribs"
ComboBox1.AddItem "--- Desserts ---"
ComboBox1.AddItem "Mango Float"
ComboBox1.AddItem "Brownies"
End Sub
Private Sub ComboBox1_Click()
Select Case ComboBox1.Value
Case "--- Main Courses ---", "--- Desserts ---"
ComboBox1.Value = ""
End Select
End Sub
Alternative 2
Use two comboboxes. One for the header and the other for the different menu items. Simply populate the second combobox based on the selection of the first combobox. For example
Private Sub UserForm_Initialize()
ComboBox1.Style = fmStyleDropDownList
ComboBox2.Style = fmStyleDropDownList
ComboBox1.AddItem "--- Main Courses ---"
ComboBox1.AddItem "--- Desserts ---"
End Sub
Private Sub ComboBox1_Click()
ComboBox2.Clear
Select Case ComboBox1.Value
Case "--- Main Courses ---"
ComboBox2.AddItem "Roast Beef with Mashed Potato"
ComboBox2.AddItem "Spicy Spareribs"
Case "--- Desserts ---"
ComboBox2.AddItem "Mango Float"
ComboBox2.AddItem "Brownies"
End Select
End Sub
Also to be on a safer side, use the ComboBox1_Change
event.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments