Loop through Checkboxes in frame

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
Is there a way I can loop through all Checkboxes in a Frame on a Userform?

All of them are named individually, so not CheckBox1, 2 etc if that makes a difference.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
sub a
Dim ctrl As MSForms.Control

For Each ctrl In Me.[COLOR=#ff0000][B]Frame1[/B][/COLOR].Controls
    If TypeName(ctrl) = "CheckBox" Then
        MsgBox "This is checkbox: " & ctrl.Name
    End If
Next ctrl
end sub
 
Upvote 0
VBA Geek,

Thanks - very helpful.....could you assist me in showing what I would add to check if any are selected?

Essentially what I now need to do is assess if there are non selected.
 
Last edited:
Upvote 0
Alternative:

You can use the TypeOf method as well....

Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As Control
    For Each Ctrl In Me.Frame1.Controls
        If TypeOf Ctrl Is MSForms.CheckBox Then
            MsgBox "Name=" & Ctrl.Name & vbCrLf & "Selected =" & Ctrl.Value
        End If
    Next
End Sub
 
Upvote 0
And, this code will inform you the unselected CheckBoxes in Frame1 of the UserForm.

Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As Control
    For Each Ctrl In Me.Frame1.Controls
        If TypeOf Ctrl Is MSForms.CheckBox Then
            If Ctrl.Value = False Then
                MyMsg = MyMsg & vbCrLf & Ctrl.Name & " is not selected"
            End If
        End If
    Next
    If MyMsg = Empty Then
        MsgBox "All of them are selected"
    Else
        MsgBox MyMsg
    End If
End Sub
 
Last edited:
Upvote 0
Alternative:

You can use the TypeOf method as well....

That might not work properly though. TypeOf would also include ToggleButtons and Optionbuttons since they also implement the Checkbox interface.
 
Upvote 0
TypeOf would also include ToggleButtons and Optionbuttons since they also implement the Checkbox interface.

I didn't know that, thanks for this info.

So, using VBA Geek's TypeName method, my last post should be modified as;

Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As Control
    For Each Ctrl In Me.Frame1.Controls
        If TypeName(Ctrl) = "CheckBox" Then
            If Ctrl.Value = False Then
                MyMsg = MyMsg & vbCrLf & Ctrl.Name & " is not selected"
            End If
        End If
    Next
    If MyMsg = Empty Then
        MsgBox "All of them are selected"
    Else
        MsgBox MyMsg
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,143
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top