Excel 用户表单按名称调用复选框
Excel Userform call checkbox by name
我有一个表单,它设置了一个标签列表,其中包含内容和一个随附的初始化复选框。
我想在单击按钮时检查复选框的值。
我如何引用复选框 - 我在创建复选框时将其称为一个数字(i 的值)。
添加复选框的代码:
Sub addLabel()
Dim theCheck As Object
Dim theLabel As Object
Dim i As Long
Dim LastRow As Integer
LastRow = Worksheets("Assumptions").Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To LastRow
Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Assumption" & i, True)
With theLabel
.Name = "Assumption" & i
.Caption = Worksheets("Assumptions").Range("B" & i).Value ' & labelCounter
.Left = 156
.Width = 500
.Top = 138 + i * 20
End With
Set theCheck = UserForm1.Controls.Add("Forms.CheckBox.1", i, True)
With theCheck
.Name = i
.Left = 140
.Width = 10
.Top = 138 + i * 20
End With
Next
End Sub
我的最终目标是检查哪个复选框是 'True',然后如果为真,则将随附的标签内容输入到工作表中。
目前我的主要问题是如何按名称引用复选框(例如,在它们被称为 1-10 的地方循环遍历它们。
谢谢
要在表单中引用对象,您可以使用以下语法
<Name of your form>.<Name of your control>
我可以相信 UserForm1.1
之类的东西,但是 只用一个数字来调用你的复选框并不是一个好主意,给它一个合适的名字 .
强烈建议您更改
With theCheck
.Name = i 'This is not great
.Left = 140
.Width = 10
.Top = 138 + i * 20
End With
通过更明确的方式,例如
With theCheck
.Name = "cb" & i 'Not great but better
.Left = 140
.Width = 10
.Top = 138 + i * 20
End With
遍历表单中的每个复选框
要遍历每个复选框并检查它是否被选中,您可以使用类似这样的东西
'Go through each control in your UserForm
For Each myControl In UserForm1.Controls
'If the current control is a Checkbox
If (TypeName(myControl) = "Checkbox") Then
'Check it's value
If (myControl.Value = True) Then
'Do whatever you want
'You can access your checkbox properties with myControl.YOUR_PROPERTY
End If
End If
Next myControl
我有一个表单,它设置了一个标签列表,其中包含内容和一个随附的初始化复选框。
我想在单击按钮时检查复选框的值。
我如何引用复选框 - 我在创建复选框时将其称为一个数字(i 的值)。
添加复选框的代码:
Sub addLabel()
Dim theCheck As Object
Dim theLabel As Object
Dim i As Long
Dim LastRow As Integer
LastRow = Worksheets("Assumptions").Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To LastRow
Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Assumption" & i, True)
With theLabel
.Name = "Assumption" & i
.Caption = Worksheets("Assumptions").Range("B" & i).Value ' & labelCounter
.Left = 156
.Width = 500
.Top = 138 + i * 20
End With
Set theCheck = UserForm1.Controls.Add("Forms.CheckBox.1", i, True)
With theCheck
.Name = i
.Left = 140
.Width = 10
.Top = 138 + i * 20
End With
Next
End Sub
我的最终目标是检查哪个复选框是 'True',然后如果为真,则将随附的标签内容输入到工作表中。
目前我的主要问题是如何按名称引用复选框(例如,在它们被称为 1-10 的地方循环遍历它们。
谢谢
要在表单中引用对象,您可以使用以下语法
<Name of your form>.<Name of your control>
我可以相信 UserForm1.1
之类的东西,但是 只用一个数字来调用你的复选框并不是一个好主意,给它一个合适的名字 .
强烈建议您更改
With theCheck
.Name = i 'This is not great
.Left = 140
.Width = 10
.Top = 138 + i * 20
End With
通过更明确的方式,例如
With theCheck
.Name = "cb" & i 'Not great but better
.Left = 140
.Width = 10
.Top = 138 + i * 20
End With
遍历表单中的每个复选框
要遍历每个复选框并检查它是否被选中,您可以使用类似这样的东西
'Go through each control in your UserForm
For Each myControl In UserForm1.Controls
'If the current control is a Checkbox
If (TypeName(myControl) = "Checkbox") Then
'Check it's value
If (myControl.Value = True) Then
'Do whatever you want
'You can access your checkbox properties with myControl.YOUR_PROPERTY
End If
End If
Next myControl