Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping Excel Macro executution when pressing Esc won't work

I'm running excel 2007 on XP.

Is there a way to stop a macro from running during its execution other than pressing escape? Usually if I think I created an infinate loop or otherwise messed something up I hit escape and it throws an error but the macro stops.

This time (and I've done it before but not this badly), I set up a msgbox for some quick debugging. Turns out it had to loop about 6000 times, which made means I had to "OK" 6000 message boxes, which took several minutes. I didn't save before running (another mistake) so I couldn't open task manager to exit.

Is there another way to stop the execution of a macro in case I goof up like this again?

like image 368
ptpaterson Avatar asked Jul 21 '11 13:07

ptpaterson


People also ask

How do I force an Excel macro to stop?

If the Macro is simply in a continuous loop or is running for too long you can use one of these keyboard shortcuts to kill it: Esc hit the Escape key. Ctrl + Break hit Ctrl key and then the break key, which is also the pause key.

How do I stop VBA from running when not responding?

You can interrupt a macro in Excel at any time by pressing Esc or Ctrl + Break.

How do I stop a VBA macro from running?

In VBA, you can stop your macro execution manually with the Esc key or by pressing Ctrl+Break.

How do I stop an infinite loop in Excel VBA?

Option 1: Hold the Esc key down for more than a few seconds. Option 2: Press CTRL+BREAK. Option 3: CTRL + ALT + DEL to end process & have Auto recover when you re-open.


2 Answers

Use CRTL+BREAK to suspend execution at any point. You will be put into break mode and can press F5 to continue the execution or F8 to execute the code step-by-step in the visual debugger.

Of course this only works when there is no message box open, so if your VBA code constantly opens message boxes for some reason it will become a little tricky to press the keys at the right moment.

You can even edit most of the code while it is running.

Use Debug.Print to print out messages to the Immediate Window in the VBA editor, that's way more convenient than MsgBox.

Use breakpoints or the Stop keyword to automatically halt execution in interesting areas.

You can use Debug.Assert to halt execution conditionally.

like image 158
Tomalak Avatar answered Oct 03 '22 08:10

Tomalak


CTRL + SCR LK (Scroll Lock) worked for me.

like image 37
VBANoob Avatar answered Oct 03 '22 09:10

VBANoob