CheckBox1.Value = False triggers CheckBox event

xzlaatc

New Member
Joined
Jun 15, 2009
Messages
15
When I use a ControlButton to reset the value of a CheckBox to 'False', it triggers the code associated with the CheckBox. Why is this happening and how do I stop it?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Presumably you have some event code connected with this Checkbox eg Change code? One way to prevent it from happening is to use a Module level boolean variable that you test in the Change event and only if True (for example) continue processing eg:

Code:
'Userform code module
 
Dim blnRunCheckBoxCode As Boolean
 
Private Sub CommandButton1_Click()
'set value of blnRunCheckBoxCode to be False so doesn't run Change code!
blnRunCheckBoxCode = False
Me.CheckBox1.Value = True
'reset blnRunCheckBoxCode:
blnRunCheckBoxCode = True
End Sub
 
 
'checkbox change code:
 
Private Sub CheckBox1_Change()
If blnRunCheckBoxCode = False Then Exit Sub  'exits sub if blnRunCheckBoxCode is False
 
'else: other code that should run
 
'...
 
End Sub

Make sense?
 
Upvote 0
Thanks for the quick response! I added the following to the ControlButton code:

ActiveSheet.NRRandomizer1.Enabled = True
Dim NRRandomizer1Code As Boolean
NRRandomizer1Code = False
ActiveSheet.NRRandomizer1.Value = False
NRRandomizer1Code = True

And I added:

If NRRandomizer1Code = False Then
Exit Sub
Else

to the CheckBox code.

The ControlButton runs as intended now, but the CheckBox no longer functions. What am I doing wrong?​
 
Upvote 0
You need to make the boolean variable a module level variable so that it persists from one sub to another (otherwise in the Checkbox code your NRRandomizer1Code variable is an undeclared variant variable and alsways returns True to the If test:

If NRRandomizer1Code = False Then

You should also place

Option Explicit

at the top of your modules so that you force yourself to declare variables (would prevent problems of this type from happening - or at the least would point you in the direction of the solution).
 
Upvote 0
Okay; I changed to addition to the CheckBox code to:

If RARandomizer1Code = False And RARandomizer1.Value = False Then
Exit Sub
Else

and it seems to be working!

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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