以编程方式在VBA(Excel)中添加ComboBox [英] Programmatically add ComboBox in VBA (Excel)

查看:226
本文介绍了以编程方式在VBA(Excel)中添加ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VBA中创建,放置和填充ComboBox的元素。我不知道自己缺少什么,但我可能无法弄清楚该怎么做。我没有在我的API中有 MSForms (即,我无法将 Dim someComboBox编写为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?

我要实现的是用户单击a按钮,将创建一个包含各种项目(组合框,单选按钮等)的新表单,因此我想以编程方式执行此操作。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆