count how many checkboxs are true

Bernieg

Board Regular
Joined
Jan 1, 2009
Messages
147
Office Version
  1. 365
Platform
  1. Windows
:)Hi

i have 4 checkboxs in a userform, but only 1 may be selected.
Wrote some code but it does not work.
any ideas ?

Bernie

Sub Validate()
'Check if more then 1 shift has been ticked
tick1 = 0
tick2 = 0
tick3 = 0
tick4 = 0
checkshift = 0
If CheckBox1.Value = True Then
tick1 = 1
ElseIf CheckBox2.Value = True Then
tick2 = 1
ElseIf CheckBox3.Value = True Then
tick3 = 1
ElseIf CheckBox4.Value = True Then
tick4 = 1
End If
checkshift = tick1 + tick2 + tick3 + tick4
If checkshift >= 1 Then
resp = MsgBox("ONLY 1 SHIFT CAN BE SELECTED", "PLEASE NOTE")
End If
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Why not use option buttons?

Then only one can be shift/option can be selected.
 
Upvote 0
There is an error in your logic. Try

Code:
Sub Validate()
'Check if more then 1 shift has been ticked
tick1 = 0
tick2 = 0
tick3 = 0
tick4 = 0
checkshift = 0
If CheckBox1.Value = True Then
tick1 = 1
End If
If CheckBox2.Value = True Then
tick2 = 1
End If
If CheckBox3.Value = True Then
tick3 = 1
End If
If CheckBox4.Value = True Then
tick4 = 1
End If
checkshift = tick1 + tick2 + tick3 + tick4
If checkshift >= 1 Then
resp = MsgBox("ONLY 1 SHIFT CAN BE SELECTED", "PLEASE NOTE")
End If
 
Upvote 0
Hi Peter

Tryed it but got Error 424 object required

Bernie
 
Upvote 0
The inequality has to be < rather than <= if one checkbox is not to be flagged.
Also, this will not flag a no checkboxes checked situation.
Code:
Sub Validate()
    Dim CheckedSum as Long
    CheckedSum = CheckBox1.Value + CheckBox2.Value + CheckBox3.Value + CheckBox4.Value

    If CheckedSum < - 1 Then
        MsgBox "One CheckBox (at most) can be checked"
    End If

End Sub
 
Last edited:
Upvote 0
hi Mike
i get the same error message object required

Bernie
 
Upvote 0
Where are you putting that code, in the userform's code module, it should work fine. If it is in a normal module, then
Code:
Sub Validate()
    Dim CheckedSum as Long
With UserForm1
    CheckedSum = .CheckBox1.Value + .CheckBox2.Value + .CheckBox3.Value + .CheckBox4.Value
End With

    If CheckedSum < - 1 Then
        MsgBox "One CheckBox (at most) can be checked"
    End If

End Sub
 
Upvote 0
your code works if no checkboxs are ticked , but not if greater then 2
I did have it in a module

Bernie
 
Upvote 0
Hi Mike
now it works fine If CheckedSum <> -1 Then
Thanks for your help, it is much appieciated

Bernie
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,628
Members
452,933
Latest member
patv

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