Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the selected text from a dropdown box

Tags:

excel

vba

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.

like image 578
karlstackoverflow Avatar asked Mar 06 '12 04:03

karlstackoverflow


People also ask

How do I get text from a dropdown list?

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.

How do I get the selected value of dropdown?

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.

How do you display the selected value of dropdown in a text box?

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.

How do I select selected text?

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.


3 Answers

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

like image 187
chris neilsen Avatar answered Oct 09 '22 07:10

chris neilsen


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
like image 36
cyberponk Avatar answered Oct 09 '22 07:10

cyberponk


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
like image 35
Niederee Avatar answered Oct 09 '22 05:10

Niederee