从另一个工作表到列表获取价值

时间:2019-05-27 23:03:51

标签: excel vba range

我正在尝试从“列表”工作表的column A中获取值,以便在“菜单”工作表的组合框中使用它。

以下是菜单表的代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect([4:7], Target) Is Nothing  And Target.Count = 1 Then
     ActiveSheet.DropDowns.Delete
        Set CB = Me.Shapes.AddFormControl(xlDropDown, Left:=Target.Left, 
        Top:=Target.Top, Width:=60, Height:=15)
        With CB
            .name = "CB"
            .OnAction = "CB_Change"
            .List = Worksheets("Listing").Range("A1").Value
        End With

    End If
End Sub

Worksheets("Listing").Range("A1").Value fails和我得到一个

  

错误438:对象不支持此属性或方法

1 个答案:

答案 0 :(得分:0)

这有效:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim lst

    If Not Intersect([4:7], Target) Is Nothing And Target.Count = 1 Then

        With Worksheets("Listing")
            lst = "'" & .Name & "'!" & _
               .Range(.Range("a1"), _
                      .Cells(.Rows.Count, 1).End(xlUp)).Address()
        End With

        Me.DropDowns.Delete
        With Me.Shapes.AddFormControl(xlDropDown, Left:=Target.Left, _
                                      top:=Target.top, Width:=60, Height:=15)
             .Name = "CB"
            .OnAction = "CB_Change"
            .ControlFormat.ListFillRange = lst
        End With

    End If
End Sub