如何通过dynamic创build的用户窗体上的checkbox循环?
我试图利用一个dynamic创build的用户窗体,并根据什么框检查,灰色的某些单元格。
作为背景,这是一个注塑设备。 QA设置正在运行的腔体编号。 这个dynamic的用户表单根据在工作表上input的腔体编号创buildcheckbox。
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
我开始做一件事,不知何故,这是我的代码变成了什么。 这不是最好的代码,但我仍然认为自己是一个新手。 另外,这是我第一次无法自己find答案,因此我第一次寻求帮助。 所以,任何帮助将不胜感激!
谢谢!
更新这太棒了! 谢谢唐。 不过,我有一个后续问题。 也许我已经长期对此感到震惊,但我想不出一个更有效的方法来做到这一点。 这是我现在正在做的事情:
For i = 2 To 17 Set ctl = Controls.Item("CheckBox_" & i) If ctl = True Then If i = 2 Then Range(Cells(j, c), Cells(m, c)).Select Selection.Interior.ColorIndex = 16 Range(Cells(k, c), Cells(m, c)).Select With Selection .Merge Cells(k, c) = "BLOCKED" .Orientation = 90 .VerticalAlignment = xlCenter .HorizontalAlignment = xlCenter End With End If Next i
等等
我正在写作,如果我从1到17,但我觉得有一个更有效的方法,我不能破解它。
按照您创build它们的相同方式按名称循环。
Dim ctl As Control For i = 2 To 17 Set ctl = Controls.Item("CheckBox_" & i) Next 'i
另外,我会为col,lcol等的初始值创build模块级常量,并在两个例程中重用这些常量。
- CheckBox来限制数据validation列表
- Excelmacroscheckbox
- 如何做几个checkbox相同的代码来设置checkbox值?
- Excel – 根据单元格内容设置Userformcheckbox的值
- Excel vba重命名checkbox
- 读取Java Apache POI中的Excelcheckbox值
- 用户自定义命令button,基于checkbox复制True / False
- 只有在checkbox被选中的情况下运行一个macros,并且当每个checkbox未被选中时,它将恢复到一个设置的“正常”状态
- Excel VBA – 当一系列后续checkbox全部打开时打开ActiveXcheckbox