Ask Your Question
0

How to break a for loop

asked 2015-12-22 15:24:28 +0200

Ulli_Wue gravatar image

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
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
0

answered 2015-12-27 17:22:21 +0200

TheDatabaseGuy gravatar image

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

edit flag offensive delete link more
0

answered 2015-12-22 17:39:24 +0200

JohnSUN gravatar image

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"

edit flag offensive delete link more
0

answered 2015-12-22 16:41:43 +0200

pierre-yves samyn gravatar image

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

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-12-22 15:24:28 +0200

Seen: 543 times

Last updated: Dec 27 '15