How to break a for loop

Hi there,

I’m wondering if it is possible to break a simple loop based on for-next (see example below).
How can this loop be stopped? Obviously, Ctrl + Break oder Ctrl + c is not working for me.
Any idea or hint is appreacheated.

Best wishes
Ulli

sub how_to_break_a_for_loop
	for i = 1 to 100
		msgbox "this is " & i
	next	
end sub

Hi

I am afraid it is not possible to break. You have to kill…

You can stop, sometimes, with Shift+Ctrl+Q (or the Stop toolbar button) but not in a loop like that, unless you insert something like:

sub how_to_break_a_for_loop
    for i = 1 to 50
	    wait 10
        msgbox "this is " & i
    next    
end sub

You must click Ok to close the msgbox and hit Shift+Ctrl+Q very quickly

Regards

Try this

Sub how_to_easily_break_a_for_loop
    For i = 1 to 100
        If Msgbox("This is " & i, 1, "Test")  = 2 Then Exit For
    Next     
End Sub 

Second parametr of MsgBox 1 = “Display OK and Cancel buttons”

1 Like

If you want the code to break itself under a certain circumstance, you can set ‘i’ to 100. That will end the loop.

1 Like