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.

  1. 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.
  2. If your keyboard has a function key, usually labeled Fn, you may have to hit Ctrl+Fn+Break to manually kill your macro.
  3. 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!

Did you find this helpful?

Please support wellsr.com by downloading an add-in below. Your support helps me continue to post VBA tutorials like this one.

$15

Mouse to Macro

Record your mouse clicks and cursor movements and convert them directly into VBA macros so you can automate your mouse.

FREE

wellsrPRO

Import hundreds of macro examples from wellsr.com without ever leaving Excel and organize your personal macro library with this free add-in.

$50

CF Shapes

Build stunning dashboards by dynamically controlling your shapes with this add-in that enables conditional formatting for shapes.


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

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:

VBA Stop Macro

All you have to do is click “End” and your macro will stop.


Comments

That’s all for today’s tutorial. Short and sweet!

Please share this article with your friends on Facebook, Twitter, and Google+. Sharing articles with others is how wellsr.com will continue to grow, so I genuinely appreciate it.

When you’re ready to free up your time in the office, visit my Excel Add-ins page for some powerful automation products.

I hope you’ll reach out to me via my VBA Consulting page if you have an idea for a macro, but you’d like some help.


About Ryan Wells

Ryan Wells

Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA products, including Mouse To Macro. See more Excel products, including online courses and books, by visiting the Excel VBA Store.