VBA loop help

dave8

Active Member
Joined
Jul 8, 2007
Messages
267
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.
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Jogender singh

New Member
Joined
May 21, 2020
Messages
14
Office Version
2013
Platform
Windows
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
 

dave8

Active Member
Joined
Jul 8, 2007
Messages
267
The problem with this is the Checkbox2.value is not getting cleared. But I can click on 'Ok' once.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,756
Office Version
365
Platform
Windows
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
 

dave8

Active Member
Joined
Jul 8, 2007
Messages
267
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,756
Office Version
365
Platform
Windows
If you change the value of the checkbox, then it triggers the click event, so the flag simply stops that.
 

Watch MrExcel Video

Forum statistics

Threads
1,095,733
Messages
5,446,188
Members
405,390
Latest member
RafalKowalski

This Week's Hot Topics

Top