Counting how many checkboxes have been selected on a userform?

chuckles1066

Banned
Joined
Dec 20, 2004
Messages
372
I have 24 checkboxes on my userform.

Is there a quicker way to count how many have been checked than
Code:
If CheckBox1.Value = TRUE then count = count+1
If CheckBox2.Value = TRUE then count = count+1
...
...
...
If CheckBox24.Value = TRUE then count = count+1

I tried
Code:
For x = 1 to 24
If CheckBox & x.Value = TRUE then count = count+1
Next

but that errored.
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You can loop through them something like this:

Code:
Dim oControl As MSForms.Control

For Each oControl In UserForm1.Controls
    Debug.Print oControl.Name & vbTab & oControl.Value
Next oControl

Gary
 
Upvote 0
Hello

See the links

http://www.pcreview.co.uk/forums/looping-through-all-checkboxs-userform-t955254.html

http://www.mrexcel.com/forum/showthread.php?t=427103


And try.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br><SPAN style="color:#00007F">Dim</SPAN> ctl <SPAN style="color:#00007F">As</SPAN> Control<br> <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ctl <SPAN style="color:#00007F">In</SPAN> UserForm1.Controls<br>  <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">TypeOf</SPAN> ctl <SPAN style="color:#00007F">Is</SPAN> MSForms.CheckBox <SPAN style="color:#00007F">Then</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> ctl <SPAN style="color:#00007F">Then</SPAN> c = c + 1<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br> <SPAN style="color:#00007F">Next</SPAN><br>  Me.Label1.Caption = c<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Code:
Sub HowManyChecks()

    Dim i As Integer
    Dim ctl As MSForms.Control
    
    For Each ctl In UserForm1.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            i = i + 1
        End If
    Next
    
    MsgBox i & " checkboxes are checked."

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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