Endless Loop

MikeDBMan

Well-known Member
Joined
Nov 10, 2010
Messages
608
Frequently when I am entering code in VBA, I will execute the code. But if I inadvertently created an endless loop sometimes my screens will go tinted white and I cannot interrupt the code. How can I stop the code without having to lose all my latest work? If I remember, I will add a doevents line in my loop which will allow me to interrupt the loop but sometimes I forget that as well. Suggestions?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You can press Ctrl+Break.

You can also add max values in a loop for example if you know a loop shouldn't run more than 1,000 times you can add this:

Code:
Do
    i = i + 1
    If i > 1000 Then Exit Do
Loop
 
Last edited:
Upvote 0
post your code. Maybe we can see why does your loop run forever and freeze.
 
Upvote 0
If I had thought of something like the max value I would have put it in there. The problem is that it is too late and the code is running in an endless loop. Ctrl-Break does NOT interrupt it. Is there any way to interrupt it after I have gotten to this point? I have inadvertently been able to stop it before losing everything before but frankly I don't know what key combination I may have hit that did the trick. Surely someone else has this same experience out there?
 
Upvote 0
Did you try Ctrl + Alt + Del?

Hit cancel once the screen comes up and try hitting Ctrl + C or ESC repeatedly.
 
Upvote 0
I cannot seem to get this suggestion to work. (Ctrl+Alt+Del) then Cancel and Ctrl-C or ESC). I did it to myself again today and had to close down Excel and revert back to the last auto-saved file. Any other suggestions?
 
Upvote 0
For me, this worked already several times: CTRL-ALT-PAUSE + F12 to stop an endless loop, when the keyboard has no BREAK-button
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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