我有一个带有动态复选框的用户表单(已编程) 以下是我的代码
Dim Rows As Integer
Dim toppart As Integer
Dim Opt As Variant
Dim x As Integer
On Error Resume Next
toppart = 20
UpdateRow = Application.WorksheetFunction.CountA(ActiveSheet.Range("C3:CU3"))
For x = 3 To UpdateRow
Set Opt = Te.Controls.Add("Forms.CheckBox.1", "CheckBox" & x, True)
Opt.Caption = ActiveSheet.Cells(x, "C").Value
Opt.Width = 70
Opt.Height = 18
Opt.Left = 18
Opt.Top = toppart
toppart = toppart + 20
Next
我知道如果通过控件设置了复选框,我的代码将如下所示:
If (CheckBox1.Value = False) Or (CheckBox2.Value = False) Then
MsgBox "You must select alteast 2 checkboxes", vbCritical
但是当动态创建复选框时,我无法想到一种有效的方法。请任何建议或帮助非常感谢,谢谢。
答案 0 :(得分:1)
未经测试,但您可以循环播放表单上的控件,检查每个控件是否为复选框,如果是,请查询.Value
并计算“已选中”复选框的总数。如果该数字为LTE 1,然后你提出警告/ MsgBox
:
Dim checked as Long
Dim ctrl as Object
For Each ctrl in Me.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then
checked = checked + 1
End If
End If
Next
If checked <= 1 Then
MsgBox "You must select alteast 2 checkboxes", vbCritical
Exit Sub
End If