以编程方式在 VBA (Excel) 中添加 ComboBox [英] Programmatically add ComboBox in VBA (Excel)
问题描述
我正在尝试在 VBA 中创建、放置和填充 ComboBox 的元素.我不明白我错过了什么,但我无法弄清楚如何去做.我没有在我的 API 中有 MSForms
(即我不能写 Dim someComboBox As MSForms.ComboBox
因为它失败了.是否可以导入它不知何故?有人能指出我正确的方向吗?
I am trying to create, place and fill in elements of a ComboBox in VBA. I don't understand what I am missing, but I cannot possibly figure out how to do it. I do not have MSForms
in my API (i.e. I cannot write Dim someComboBox As MSForms.ComboBox
as it fails. Is it possible to import it somehow? Can someone point me in the right direction?
我想要实现的是,当用户单击按钮时,会创建一个包含各种项目(组合框、单选按钮等)的新表单,因此我想以编程方式执行此操作.
What I want to achieve is that when the user clicks a button a new form is created with various items (ComboBoxes, RadioButtons, etc.) and thus I want to do this programmatically.
推荐答案
你可以依赖以下代码:
Set curCombo = ActiveSheet.Shapes.AddFormControl(xlDropDown, Left:=Cells(1, 1).Left, Top:=Cells(2, 1).Top, Width:=100, Height:=20)
With curCombo
.ControlFormat.DropDownLines = 2
.ControlFormat.AddItem "Item 1", 1
.ControlFormat.AddItem "item 2", 2
.Name = "myCombo"
.OnAction = "myCombo_Change"
End With
这篇关于以编程方式在 VBA (Excel) 中添加 ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!