简体   繁体   English

在Excel 2013上使用VBA填充组合框(窗体控件)

[英]Populate Combobox(form control) using VBA on Excel 2013

I am new to VBA. 我是VBA的新手。 I have been trying for hours now to execute a simple function of populating a combobox(form control) using VBA code. 我已经尝试了几个小时,以执行使用VBA代码填充组合框(窗体控件)的简单功能。 I have looked on a lot of websites(including this one), but none of the codes seem to work. 我看过很多网站(包括该网站),但似乎没有任何代码有效。 I have been using this code. 我一直在使用此代码。 (I am putting this code in a module) (我将此代码放入模块中)

Sub populateDropDown303()

With Worksheets("S1 Fuel Consumption").Shapes("Drop Down 303").ControlFormat

.AddItem "this"

.AddItem "that"

End With

End Sub

Hey I got the code working. 嘿,我的代码起作用了。 But every time I select a value from the the combobox dropdown, it runs the code again and displays the duplicate values. 但是每次我从组合框下拉列表中选择一个值时,它都会再次运行代码并显示重复的值。 How do I remove that ? 我该如何删除?

Try this: 尝试这个:

Sub populateDropDown303()
    Dim ws As Worksheet: Set ws = Worksheets("S1 Fuel Consumption")
    With ws.Shapes("Drop Down 303").ControlFormat
        .RemoveAllItems '~~> This is what you lack I think
        .AddItem "This"
        .AddItem "That"
    End With
End Sub

I added a new variable ws of Worksheet type so Intellisense kicks in. 我添加了一个新的Worksheet类型变量ws ,以便Intellisense加入。
That way, it will be easier for you to see the available methods and properties for the object you're working on. 这样,您将更容易查看正在处理的对象的可用方法和属性。 HTH. HTH。

With Worksheets("S1 Fuel Consumption").Dropdowns("Drop Down 303")
    .AddItem "this"
    .AddItem "that"
End with

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

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