简体   繁体   English

如何在excel中使用vba创建和填充activex组合框。

[英]How to create and populate an activex combobox using vba in excel.

I am having a problem when trying to create and then populate a activex combobox in vba for excel. 我在尝试创建然后在vba中为excel填充activex组合框时遇到问题。 The code below works when run as two seperate macros but when I try to put the two together an empty combobox is created. 下面的代码在作为两个单独的宏运行时起作用,但是当我尝试将两者放在一起时,会创建一个空的组合框。 Can anybody tell me why this is and how to overcome this problem? 任何人都可以告诉我为什么会这样,以及如何克服这个问题?

Thanks in advance, JW 在此先感谢,JW

 Sub CreateComboBox1()
    'Creating ComboBox1:
    ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
                Link:=False, DisplayAsIcon:=False, Left:=50, Top:=80, Width:=100, _
                Height:=15).Select
    End Sub

    Sub PopulateComboBox1()
    'Populating ComboBox1
    Sheet1.ComboBox1.AddItem "Date", 0
    Sheet1.ComboBox1.AddItem "Player", 1
    Sheet1.ComboBox1.AddItem "Team", 2
    Sheet1.ComboBox1.AddItem "Goals", 3
    Sheet1.ComboBox1.AddItem "Number", 4
    End 

Try this 尝试这个

Sub CreateComboBox1()
    With ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
                Link:=False, DisplayAsIcon:=False, Left:=50, Top:=80, Width:=100, _
                Height:=15)
        With .Object
            .AddItem "Date"
            .AddItem "Player"
            .AddItem "Team"
            .AddItem "Goals"
            .AddItem "Number"
        End With
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM