Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
903 views
in Technique[技术] by (71.8m points)

vba - Return the selected text from a dropdown box

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...