I am trying to build a code which will warn the user with a message box if all checkboxes are left empty in my userform.
Any help on how to refer to all checkbox controls in one userform to build out this code would be appreciated.
Below is an example of the code I have tried, but it does not work.
Dim Allcheckbox As Variant
Allcheckbox = Array("checkbox1", "checkbox2", "checkbox3", "checkbox4", "checkbox5", "checkbox6")
If Controls(Allcheckbox).Value = False Then
MsgBox ("Please select a comparison criteria.")
End If
I would loop through the array and check each value of the checkboxes. If any are have value = true then we set a boolean to true. This would be better in a function and you can pass your array and then you can use Exit Function as soon as true is passed.
ctrlSelect = False
For Each ctrl In form.Controls
If TypeName(ctrl) = "CheckBox" Then
If crtl.value = "True" Then
ctrlSelect = True
End If
End If
Next ctrl
If Not ctrlSelect Then
MsgBox ("Please select a comparison criteria.")
Exit Sub
End If
Thanks for your help Asher!
I ended up using this code below which provided a solution.
Dim x As Integer
x = 0
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
If cCont.Value = True Then
x = x + 1
End If
End If
Next
If x = 0 Then
MsgBox ("Please select a comparison criteria.")
End If
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.