selected checkbox deselects other checkboxes and vice versa

looping

New Member
Joined
Jun 10, 2011
Messages
18
Hi everyone,

I have an excel sheet with three checkboxes, of which one is selected (checkbox.value = true) and the others not. Selecting one checkbox shall deselect the other ones (and do some other stuff that is not relevant here).

I tried this:
Code:
Private Sub checkbox1_Click()
    checkbox1.value = True
    checkbox2.value = False
    checkbox3.value = False
End Sub
My problem is that checkbox_Click() is also triggered if the checkbox-value is changed via VBA, resulting in a loop with the checkboxes selecting and deselecting each other.

So, I am looking for an alternative Trigger event for the Sub, but I can't think of anything that would work.

Thanks in advance.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You could try subsetting the conditions with an IF statement. Something like:

Code:
Private Sub checkbox1_Click()
If  checkbox1.value = True Then
    checkbox2.value = False
    checkbox3.value = False
end If
End Sub

Private Sub checkbox2_Click()
If  checkbox2.value = True Then
    checkbox1.value = False
    checkbox3.value = False
end If
End Sub

Private Sub checkbox3_Click()
If  checkbox3.value = True Then
    checkbox1.value = False
    checkbox2.value = False
end If
End Sub

That should keep everything from re-triggering.
 
Upvote 0
Thanks! There are no loops any more. (You can deselect all checkboxes now, but I can live with that)
 
Upvote 0
If you don't want to be able to deselect all of the checkboxes, maybe you should use a radiobutton...
 
Upvote 0
The problem is caused by events triggering each other, a well-known phenomenon, and cause of many a head-ache

use application.enableevents = false at the start of your routine, then reapplying at the end. Good discipline on this feature will save you many problems in future ;)
 
Upvote 0
I could be wrong, but doesn't application.enableevents only apply to worksheet-level events? I don't think that would work on a userform. I guess I could just go and test it, but that's my gut feeling.
 
Upvote 0
This may be totally irrelevant, but if you use optionbuttons instead, they have this property already (selecting one deselects the others). You need to make sure that they all have the same GroupName in the properties (this is important if you have more than one set).
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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