I am trying to return the text from a dropdown box that is selected on an Excel form. I have tried many things and the closest I have gotten is returning the index number. Also had a look at:
Link: Return the text from a dropdown box rather than the index number
I haven't found a working solution on that page. I have tried things such as:
ActiveSheet.DropDowns("DropDown1").Value
ActiveSheet.DropDowns("DropDown1").Text
ActiveSheet.DropDowns("DropDown1").SelectedValue
ActiveSheet.Shapes("DropDown1").Value
etc.
We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
You can bind a selected value from the drop-down list using the ngModel and edit it inside the text box or numeric input box. The selected item text and value can be got using the change event of the drop-down list through the args. itemData.
To select a single word, quickly double-click that word. To select a line of text, place your cursor at the start of the line, and press Shift + down arrow. To select a paragraph, place your cursor at the start of the paragraph, and press Ctrl + Shift + down arrow.
This will return the current selection from the DropDown
Sub TestDropdown()
Dim ws As Worksheet
Dim dd As DropDown
Set ws = ActiveSheet
Set dd = ws.Shapes("DropDown1").OLEFormat.Object
MsgBox dd.List(dd.ListIndex)
End Sub
BTW, assigning to a variable declared as Dim dd As DropDown
will give you intellisense on dd
You can also get the caller name, if the macro is called by the dropdown box itself. This way you don´t have to worry about renaming the dropdown boxes :)
Sub Dropdown_OnSelect()
Dim dd As DropDown
Set dd = ActiveSheet.Shapes(Application.Caller).OLEFormat.Object
MsgBox dd.List(dd.ListIndex)
End Sub
If you are unable to Dim as DropDown
I found that this alteration will work.
Sub TestDropdown()
Dim ws As Worksheet
Dim dd As Object
Set ws = ActiveSheet
Set dd = ws.DropDowns("DropDown1")
MsgBox dd.List(dd.ListIndex)
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With