at least 5 check box bus be true out of 27 checkbox

sachin katariya

New Member
Joined
Jul 13, 2020
Messages
21
Office Version
  1. 2013
Platform
  1. Windows
Private Sub CheckBox1_Change()
Dim Count As Integer
Count = 0
If CheckBox1.Value = True Then Count = Count + 1
If CheckBox2.Value = True Then Count = Count + 1
If CheckBox3.Value = True Then Count = Count + 1
If CheckBox4.Value = True Then Count = Count + 1
If CheckBox5.Value = True Then Count = Count + 1
If CheckBox6.Value = True Then Count = Count + 1
If CheckBox7.Value = True Then Count = Count + 1
If CheckBox8.Value = True Then Count = Count + 1
If CheckBox9.Value = True Then Count = Count + 1
If CheckBox10.Value = True Then Count = Count + 1
If CheckBox11.Value = True Then Count = Count + 1
If CheckBox12.Value = True Then Count = Count + 1
If CheckBox13.Value = True Then Count = Count +1
If Count > 8 Then CheckBox1.Value = False
End Sub

Please any one can simplify this
 

Attachments

  • Untitled.png
    Untitled.png
    15.5 KB · Views: 10

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You could write a loop, but the syntax of that would depend on what kind of checkboxes these are
Other than that it would be a little faster to replace this throughout.
VBA Code:
' replace
If CheckBox1.Value = True Then Count = Count + 1 
' with
Count = Count - CLng(CheckBox1.Value)
 
Upvote 0
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
j = j + 1
End If
End If
Next
If j > 6 Or j < 6 Then
MsgBox "Subject should be FIVE"
Exit Sub
End If
You could write a loop, but the syntax of that would depend on what kind of checkboxes these are
Other than that it would be a little faster to replace this throughout.
VBA Code:
' replace
If CheckBox1.Value = True Then Count = Count + 1
' with
Count = Count - CLng(CheckBox1.Value)
 
Upvote 0
That code looks like you are dealing with a userform and its checkboxes.
(It also is putting up a message box about "five" whenever the count of checked boxes is different than 6)

VBA Code:
Dim oneControl as MSForms.Control
Dim CheckedCount as Long

For Each oneControl in Userform1.Controls
    If TypeName oneControl = "CheckBox" Then
        CheckedCount = CheckedCount - CBool(oneControl.Value)        
    End If
Next oneControl

If CheckedCount < 5 Then
    MsgBox "less than 5 checked"
End If
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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