VBA Stop Macro
I accidentally created an infinite loop macro. How can I stop the macro from running?
This is a question I get asked quite often. Fortunately, the solution is easy to remember!
How to manually stop your macro
In VBA, you can stop your macro execution manually with the Esc key or by pressing Ctrl+Break.
Here are a couple alternatives if the first two keystroke options fail.
- If you’re running a macro that uses a lot of processing power, you may need to repeatedly press the Esc or Ctrl+Break keys before it’s finally recognized.
- If your keyboard has a function key, usually labeled Fn, you may have to hit Ctrl+Fn+Break to manually kill your macro.
- If none of these options work, your last resort is to kill the Excel application in its entirety. One way to do this is to press Ctrl+Alt+Delete, launch the Task Manager, click “Details” or “Processes” and scroll down until you find Excel. Once you find it, click it and select “End task.” This is certainly not the desired way to stop your macro because you’ll lose all your unsaved work!
Stopping an Infinite Loop Demo
Sub InfiniteLoop() 'If you don't want an infinite loop, do not run this macro. Dim i As Long Do i = i + 1 Loop End Sub
Make powerful macros with our free VBA Developer Kit It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit full of pre-built macros so you can master file I/O, arrays, strings and more - grab a copy below.
It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit full of pre-built macros so you can master file I/O, arrays, strings and more - grab a copy below.
Running the macro above creates an infinite loop. If you’re brave and you want to test out the keystrokes recommended above, now’s your chance.
Let’s try it! Run the macro and try pressing Esc or Ctrl+Break. You should get a dialog box that looks like this:
All you have to do is click “End” and your macro will stop.
That’s all for today’s tutorial. Short and sweet! When you’re ready to take your VBA to the next level, subscribe using the form below.