我是VBA的新手。我现在已经尝试了几个小时来执行使用VBA代码填充组合框(表单控件)的简单功能。我看过很多网站(包括这个网站),但是这些代码似乎都没有用。我一直在使用这段代码。 (我将此代码放在一个模块中)
Sub populateDropDown303()
With Worksheets("S1 Fuel Consumption").Shapes("Drop Down 303").ControlFormat
.AddItem "this"
.AddItem "that"
End With
End Sub
嘿,我得到了代码。但每次我从组合框下拉列表中选择一个值时,它会再次运行代码并显示重复值。我该如何删除?
答案 0 :(得分:2)
试试这个:
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
我添加了ws
类型的新变量Worksheet
,因此Intellisense启动。
这样,您就可以更轻松地查看正在处理的对象的可用方法和属性。 HTH。
答案 1 :(得分:0)
With Worksheets("S1 Fuel Consumption").Dropdowns("Drop Down 303")
.AddItem "this"
.AddItem "that"
End with