如何使用 VBA 在表单上动态添加单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2049347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I dynamically add a radio button on a form using VBA
提问by melculetz
I want to dynamically add a radio button on a form, using VBA.
我想使用 VBA 在表单上动态添加一个单选按钮。
I tried writing this code, but it crashes with 'Type Mismatch'
我尝试编写此代码,但它因“类型不匹配”而崩溃
Dim optionBtn As OptionButton
Set optionBtn = UserForm1.Controls.Add("Forms.OptionButton.1", "name", True)
optionBtn.Left = 10
optionBtn.Top = 10
optionBtn.Width = 30
optionBtn.Group = "q1"
I also tried doing this:
我也试过这样做:
Dim optionBtn As Control
Set optionBtn = UserForm1.Controls.Add("Forms.OptionButton.1", "name", True)
optionBtn.Left = 10
optionBtn.Top = 10
optionBtn.Width = 30
optionBtn.Group = "q1"
but I get a Control, not a OptionButton - how can I cast it to a OptionButton ? (sorry, I'm new to VB)
但我得到了一个 Control,而不是一个 OptionButton - 我怎样才能将它转换为一个 OptionButton ?(抱歉,我是 VB 新手)
回答by Mark Biek
I was able to get it work with this (Excel 2003):
我能够让它与这个(Excel 2003)一起工作:
Dim lbl As Variant
Set lbl = UserForm1.Controls.Add("Forms.Label.1", "lblFoo", True)
lbl.Caption = "bar"
Update to reflect your change from a Label to an OptionButton
更新以反映您从 Label 到 OptionButton 的更改
Again, the key is use a Variant type for the variable that you are assigning the returned control to:
同样,关键是使用 Variant 类型作为您将返回的控件分配给的变量:
Dim opt As Variant
Set opt = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo", True)
opt.Caption = "Bar"
Keep in mind that autocomplete won't work on the variables that are defined as Variants. However you can still refer to properties and methods of those variables by typing them manually.
请记住,自动完成功能不适用于定义为 Variants 的变量。但是,您仍然可以通过手动键入来引用这些变量的属性和方法。
回答by Joshua Matisoff
Actually, I believe your problem lies in the fact that you are naming optionBtn as an object button. It needs to be named as a MSForms Option Button. Since a Variant can be an object, it will work when using a variant.
实际上,我相信您的问题在于您将 optionBtn 命名为对象按钮。它需要被命名为 MSForms 选项按钮。由于 Variant 可以是一个对象,因此在使用 Variant 时它会起作用。
I used the following and it works fine.
我使用了以下方法,效果很好。
Dim TempForm As Object
Dim newOptionButton as MSForms.OptionButton
Dim sUserformName as string
Dim i as integer
Dim x as integer
Dim y as integer
' other junk removed for example sake...
sUserformName = sheet1.cells(1,1)
' create form
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With TempForm
.Properties("Caption") = sUserformName
.Properties("Width") = 450
.Properties("Height") = 300
End With
for i = 3 to sheet1.range("A65536").End(XlUp).Row
sDefault = sheet1.cells(i,5)
iGN = sheet1.cells(i,6)
' additional code removed... for sake of example... the code would add labels, text boxes, etc...
Set newOptionButton = TempForm.designer.Controls.Add("Forms.optionbutton.1")
With newOptionButton
.Caption = sDefault
.GroupName = "Group" & iGN
.Top = 20 + (20 * x)
.Left = y
.Height = 16
.Font.Size = 8
.Font.Name = "Ariel"
End With
' here the code changes x and y depending on where the user (excel template) directs the next control.
' 这里的代码根据用户(excel 模板)指向下一个控件的位置更改 x 和 y。
next i
接下来我
Good luck....
祝你好运....
回答by guitarthrower
mark's code should work, but I often prefer to create the item manually then show/hide it based on the need.
标记的代码应该可以工作,但我通常更喜欢手动创建项目,然后根据需要显示/隐藏它。
回答by Andy Pope
You need to define the object as an optionbutton from the msforms library.
您需要将该对象定义为来自 msforms 库的选项按钮。
Dim optionBtn As MSForms.OptionButton
Set optionBtn = UserForm1.Controls.Add("Forms.OptionButton.1", "name", True)