从下拉框中返回所选文本

时间:2012-03-06 04:44:59

标签: excel vba excel-vba

我正在尝试从Excel表单上选择的下拉框中返回文本。我尝试了很多东西,而我得到的最接近的是返回索引号。还看了看:

链接:Return the text from a dropdown box rather than the index number

我在该页面上找不到可行的解决方案。我尝试过这样的事情:

ActiveSheet.DropDowns("DropDown1").Value
ActiveSheet.DropDowns("DropDown1").Text
ActiveSheet.DropDowns("DropDown1").SelectedValue
ActiveSheet.Shapes("DropDown1").Value

3 个答案:

答案 0 :(得分:10)

这将从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,分配给声明为Dim dd As DropDown的变量会在dd

上给你智能感知

答案 1 :(得分:5)

如果下拉框本身调用了宏,您也可以获取调用者名称。这样您就不必担心重命名下拉框了:))

    static DateTime GetBusinessDay(int days)
    {
        var dateTime = DateTime.Now;
        bool run = true;

        int i = 0;

        while (run)
        {
            dateTime = dateTime.AddDays(1);

            if (dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday)
            {
                continue;
            }

            i++;

            if (i == 10)
            {
                run = false;
            }
        }

        return dateTime;
    }

答案 2 :(得分:2)

如果您无法Dim as DropDown我发现此更改将有效。

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