VBA click event to Uncheck checkboxes when checking one checkbox

nmmounir

New Member
Joined
Jul 26, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I have created 7 Active x-control checkboxes. I would like to uncheck the other 6 checkboxes when the first one is checked.
if I check All Scenarios then Scenario 1, 2, 3, 4, 5 and 6 should become unchecked.

I am not good at VBA bur I tried to write this code but abviously it is not working.

Private Sub CheckBox1_Click()

Dim i As Byte

For i = 2 To Me.OLEObjects.Count

If Me.CheckBox1 = xlOn Then
Me.OLEObjects(i).Value = False

End If

Next i

End Sub


1691862555680.png
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try

VBA Code:
Private Sub CheckBox1_Click()
 For j = 2 To 7
   ActiveSheet.OLEObjects("CheckBox" & j).Object = False
 Next
End Sub
 
Upvote 0
I think you forgot the If True part? If you set box 1 to false that code will uncheck the others if they are checked. Maybe not what is wanted, but maybe OK anyway because there might be code that won't allow that to happen.
 
Upvote 0
I questioned that explanation too...

So

VBA Code:
Private Sub CheckBox1_Click()
 With ActiveSheet
   If Not .OLEObjects("CheckBox1").Object Then Exit Sub
   For j = 2 To 7
      .OLEObjects("CheckBox" & j).Object = False
   Next
 End With
End Sub
 
Last edited:
Upvote 0
To me it is clear
I would like to uncheck the other 6 checkboxes when the first one is checked
but I have been wrong before. I tend to plan for someone adding controls to a 'group' along the way so I'm more like (untested)

VBA Code:
Dim Cbox As Object
If Checkbox1 = True Then
   For Each Cbox In ActiveSheet.OLEObjects
       If Cbox.Name <> "Checkbox1" Then Cbox.Object.Value = False
   Next
End If
EDIT - although the counter is best if x out of the other remaining y checkboxes need changing and not just all of the remaining checkboxes.
 
Upvote 0
Adjusted my previous one

VBA Code:
Private Sub CheckBox1_Click()
 dim j as long
 If Not CheckBox1 Then Exit Sub
 For j = 2 To 7
    ActiveSheet.OLEObjects("CheckBox" & j).Object = False
 Next
End Sub
 
Upvote 0
Solution
Adjusted my previous one

VBA Code:
Private Sub CheckBox1_Click()
 dim j as long
 If Not CheckBox1 Then Exit Sub
 For j = 2 To 7
    ActiveSheet.OLEObjects("CheckBox" & j).Object = False
 Next
End Sub
Thank you very much. It works perfectly. one little issue:
I will need to create another separate click event for the other 6 checkboxes to uncheck all Scenarios when Scenarion1 or 2 or 3 or 4 or 5 or 6 is checked.

do I create a separate click event for each checkbox?
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

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