如何使用宏将数据从一个表行绑定到行结束到组合框中

时间:2013-03-19 06:35:28

标签: excel excel-vba vba

请为我提供此问题的解决方案。 我是宏的新手我无法做这个操作。

我试过这段代码

Sub BindCombo()
Dim Last

Last = Sheets("Defect Dump").Cells(2, 2).End(xlDown).Row
With ComboBox1
For Row = 2 To Last
.AddItem Sheets("Defect Dump").Cells(Row, 1)
Next Row
End With

End Sub

但此代码显示对象错误

1 个答案:

答案 0 :(得分:0)

您需要创建对ComboBox

的对象引用

试试这个

Sub BindCombo()
    Dim Last As Long, rw As Long
    Dim sh As Worksheet
    Dim cb As ComboBox

    Set sh = Sheets("Defect Dump")
    Last = sh.Cells(2, 2).End(xlDown).Row
    ' get Combobox on "project sheet" sheet
    Set cb = Sheets("project sheet").Shapes("ComboBox1").DrawingObject.Object
    With cb
        .Clear ' remove any existing items
        For rw = 2 To Last
            .AddItem sh.Cells(rw, 1)
        Next
    End With

End Sub