在Excel 2013中使用VBA填充Combobox(表单控件)

我是VBA新手。 我一直在尝试几个小时来执行一个使用VBA代码填充combobox(表单控件)的简单函数。 我看了很多网站(包括这一个),但没有任何代码似乎工作。 我一直在使用这个代码。 (我把这个代码放在一个模块中)

Sub populateDropDown303() With Worksheets("S1 Fuel Consumption").Shapes("Drop Down 303").ControlFormat .AddItem "this" .AddItem "that" End With End Sub 

嘿,我的代码工作。 但每次我从combobox下拉列表中select一个值时,它将再次运行代码并显示重复值。 我如何删除?

尝试这个:

 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 

我添加了一个新的Worksheettypes的variablesws ,所以Intellisense开始了。
这样,您将更容易看到正在处理的对象的可用方法和属性。 HTH。

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