当我尝试在工作表

时间:2018-01-09 18:06:00

标签: excel vba excel-vba combobox

我有一个DialogSheet(不是用户形式,这就是近20年前工作表的设置方式),我已经添加了一个组合框。我试图将该组合框的值保存到我的工作表上的单元格但是我收到以下错误:

  

运行时错误'438':对象不支持此属性或方法。

以下是导致错误的子项:

Sub ModelNameSelection_Change()

Dim ModelName As String

     ModelName = DialogSheets("setup").ModelNameSelection.Value

     Worksheets("sheet1").Unprotect

     Worksheets("sheet1").Cells(1, 10).Value = ModelName

     Worksheets("sheet1").Protect 

End Sub

组合框所在的对话框称为“setup”,组合框的名称为ModelNameSelection。

是否有特定的方法来引用下拉框中的值?

2 个答案:

答案 0 :(得分:1)

替换你的行:

ModelName = DialogSheets("setup").ModelNameSelection.Value

使用:

ModelName = DialogSheets("setup").DropDowns("ModelNameSelection").Value
编辑:哎呀我没有看到@Tim Williams的评论有相同的答案

答案 1 :(得分:0)

来自this post - 这应该让你开始:

Dim sheet As Object ' As DialogSheet?
Set sheet = DialogSheets("setup")
Dim shp As Excel.Shape
For Each shp In sheet.Shapes
    If shp.Type = msoFormControl Then
        If shp.FormControlType = XlFormControl.xlDropDown Then
            'todo
        End If
    End If
Next

换句话说,您可以通过工作表的Shapes集合访问控件。由于您知道控件的确切名称,因此可以按名称检索形状,而不是全部迭代:

Set shp = sheet.Shapes("ModelNameSelection")
Debug.Assert shp.FormControlType = XlFormControl.xlDropDown

从那里使用其ControlFormat属性来获取控件的Value

ModelName = shp.ControlFormat.Value