views:

163

answers:

3

I created a dropdown by dragging the combo box onto my sheet from the UserForm toolbar. I assigned some values to it from some cells in the book. Now I want some vba code to access the selected dropdown item's value in form of a string.

My dropdown contains only text.

Also how do I find the name of this newly created dropdown...it's no where in the properties!

Thanks!

A: 

This is a clunky way of doing it but it should work:

Dim o As Object

For Each o In Worksheets("Sheet1").Shapes
    MsgBox o.Name
Next o

There is also a hidden DropDowns collection member of the Worksheet object that you could iterate over. This will find items inserted from the Forms toolbar but won't find items inserted from the Control Toolbox toolbar

barrowc
A: 

Here's how you get the String without needing to know the name:

Dim DD As Shape

Set DD = ActiveSheet.Shapes(Application.Caller)

MsgBox DD.ControlFormat.List(DD.ControlFormat.ListIndex)
Lance Roberts
I get a type mismatch on the 2nd line. Again I don't know how it works but I'm using the dropdown control not the user form, perhaps that changes how we normally access the string.
Ehsan
I tested mine by clicking the ComboBox control on the Forms toolbar, then clicking on the sheet to add it. I then created the DropDown1_Change sub on Sheet 1 with this code in it. Then I went back to the Combobox, right-clicked and chose Assign Macro and picked the relevant Macro I'd just created. Then I right-clicked and picked Format Control, and chose the input range of A1:A3. I then put some values in A1, A2 and A3. Then when I clicked on the ComboBox and chose one of those values, a Message box appeared with the value.
Lance Roberts
Lance, thanks for the detailed step by step. I tried your instructions Again and I still received the same error. I did however post the working answer already beforehand which is intuitive and clearer. That method allows me to see what I'm calling and identifying the types of objects I'm using (i.e DropDown). So as a programmer I'm not just seeing generic shape objects and Application Caller. I like your method for a more generic scenario rather than a specific drop down or 2 drop downs that filter some data. Nevertheless Thanks for your effort!
Ehsan
A: 
Dim dd As DropDown
Set dd = ActiveSheet.DropDowns("Drop Down 6") 
Set r = Sheet2.Range(dd.ListFillRange)

Set ddValue = r(dd.Value)

NOTES:

  • DropDown is not a visible class. You just use it and it works.

  • To find the name of the dropdown CONTROL (not userform) just look at
    the name box in the top left corner of your screen just above column A. It says the name of the control when you right click on your control.-

  • Sheet2 is where the dropdown list is populated. So wherever your list data is.

    Hope that helps you all.

Ehsan