VBA loop help

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
Please help me explain this loop problem.

Checkbox2_click()
If Checkbox1.value = false then
msgbox "something..."
'clear the check box
Checkbox2.value = false
exit sub
end if
End Sub

It seems like this is looping twice. How do I get out of this loop by clicking 'OK' once? I have to click 'OK' twice.

Thank you to help me resolve this simple problem.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
you need to change it

Private Sub CheckBox2_Click()

If CheckBox1.Value = False Then
MsgBox "something..."
'clear the check box
Exit Sub
CheckBox2.Value = False
End If
End Sub
 
Upvote 0
The problem with this is the Checkbox2.value is not getting cleared. But I can click on 'Ok' once.
 
Upvote 0
How about
VBA Code:
Private Sub CheckBox2_Click()
   Static Flg As Boolean
   
   If Flg Then Exit Sub
   If CheckBox1.Value = False Then
      Flg = True
      MsgBox "something..."
      'clear the check box
      CheckBox2.Value = False
      Flg = False
   End If
End Sub
 
Upvote 0
YES!!! Thank you. Works the way I want. I can now understand why a flag is needed. I was also trying to call a sub procedure and clearing the CheckBox2 and exiting Sub upon the return
and still didn't work.
 
Upvote 0
If you change the value of the checkbox, then it triggers the click event, so the flag simply stops that.
 
Upvote 0

Forum statistics

Threads
1,214,594
Messages
6,120,436
Members
448,964
Latest member
Danni317

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