【问题标题】:VBA: Cannot add item to comboboxVBA:无法将项目添加到组合框
【发布时间】:2014-07-15 17:28:27
【问题描述】:

我正在尝试通过 VBA 将一些项目添加到下拉列表中。每当我设置组合变量时,我都会收到 13 错误。 我无法将组合变量设置为 libro 表中的组合框,因此我不能使用 combobox.additem 属性,我该如何设置它?

Sub Prueba()
Dim libro As Worksheet
Dim combo As ComboBox

Set libro = ActiveWorkbook.Sheets("Tabla Paquetes")
Set combo = libro.Shapes("ComboBox1")

    With combo
        .AddItem "Paris"
        .AddItem "New York"
        .AddItem "London"
    End With

End Sub

【问题讨论】:

    标签: excel vba combobox


    【解决方案1】:

    您的组合框不是形状,因此您的代码不会在 Shapes 集合中找到它。

    相反,请执行以下操作:

    Sub Prueba()
    
        Dim libro As Worksheet
        Dim combo As ComboBox
    
        Set libro = ActiveWorkbook.Sheets("Tabla Paquetes")
        'Set combo = libro.Shapes("ComboBox1")
    
        With ComboBox1 ' assuming the name of your control is "ComboBox1"
            .AddItem "Paris"
            .AddItem "New York"
            .AddItem "London"
        End With
    
    End Sub
    

    【讨论】:

    • 谢谢,它成功了。但是为什么我不必为组合框设置一个 var?
    • @toku_mo 因为它是一个命名对象。你可以说同样的方式,例如,ActiveWorkbook
    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多