Excel 如何在动态创建的userform上循环检查复选框?
我正试图利用一个动态创建的用户表单,并根据复选框,灰显某些单元格 作为背景,这是一个注塑设备。QA设置正在运行的型腔编号。此动态用户表单根据工作表上输入的型腔编号创建复选框Excel 如何在动态创建的userform上循环检查复选框?,excel,vba,for-loop,checkbox,userform,Excel,Vba,For Loop,Checkbox,Userform,我正试图利用一个动态创建的用户表单,并根据复选框,灰显某些单元格 作为背景,这是一个注塑设备。QA设置正在运行的型腔编号。此动态用户表单根据工作表上输入的型腔编号创建复选框 Option Explicit Private Sub UserForm_Initialize() Dim col As Long Dim row As Long Dim lcol As Long Dim i As Long Dim j
Option Explicit
Private Sub UserForm_Initialize()
Dim col As Long
Dim row As Long
Dim lcol As Long
Dim i As Long
Dim j As Long
Dim chkBox As MsForms.CheckBox
Dim l As MsForms.Frame
Dim t As MsForms.Label
Set l = Me.Controls.Add("Forms.Frame.1", "cavz", True)
l.Caption = "BLOCKED CAVITIES"
l.Height = 195
Set t = l.Controls.Add("Forms.Label.1", "mark")
t.Caption = "Mark all cavities that are currently blocked:"
t.Width = 175
t.Top = 10
col = 2 'Set your column index here
row = 8
lcol = 17
j = 1
For i = col To lcol
Set chkBox = l.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
If Worksheets("QA").Cells(row, i).Value <> "" Then
chkBox.Caption = Worksheets("QA").Cells(row, i).Value
ElseIf Worksheets("QA").Cells(row, i).Value = "" Then
GoTo 10
End If
If i <= 9 Then
'MsgBox "i = " & i
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
ElseIf i > 9 Then
j = j + 1
'MsgBox "j = " & j
chkBox.Left = 100
chkBox.Top = 5 + ((j - 1) * 20)
End If
10
Next i
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub CommandButton1_Click()
Dim x As Control
Dim cavz As MsForms.Frame
For Each x In cavz.Controls
If x.Value = True Then
If x.Value = Range("B8") Then
Range("B8:B14").Select
Selection.Interior.ColorIndex = 16
End If
End If
Next x
End Sub
等
如果I=1到17,我正在写,但我觉得有一种更有效的方法,我就是无法破解 以创建它们的相同方式按名称循环它们
Dim ctl As Control
For i = 2 To 17
Set ctl = Controls.Item("CheckBox_" & i)
Next 'i
此外,我还将为col、lcol等的初始值创建模块级常量,并在这两个例程中重用它们。Hi Don!谢谢你。我只是在我原来的问题上添加了一个更新,想看看你是否有任何建议。再次感谢!我不明白您在后续操作中要做什么。这将选择单元格并更改其颜色,如果在用户窗体上选择为阻止,则添加文本“阻止”。我有16条if语句,我只是好奇是否有更好/更有效的方法。我不认为if语句有什么理由。找出每种情况下的变化,并使用循环每次设置c、m、j和k的增量值。
Dim ctl As Control
For i = 2 To 17
Set ctl = Controls.Item("CheckBox_" & i)
Next 'i