Code Execution Has Been Interrupted

Debug code execution has been interrupted meme

“Code execution has been interrupted”

Have you ever gotten this error? I think it’s probably one of the worst Excel errors EVER.

You press continue, but the dialog keeps reappearing. You’ve looked through your code and found no errors…and yet, the box will not go away.

🤔 So what’s happening?

I’ve found this error often appears in VBA code using loops. It starts like this. You accidentally create an infinite loop (it happens), so you use CTRL + Pause Break and then tell Excel to end execution.

Or, a runtime error results in your loop. Either way, you tell Excel to end execution rather than fix the code and continue. Unfortunately, that breakpoint is written somewhere in memory and is not cleared when you stop your code.

🛠️ Here’s how to fix it:

Option 1 - Save and then restart your computer.

Option 2 - Hit “Debug” when the error appears, and then hit CTRL + Break again to clear the memory within Excel that’s storing the error. Then let the code run error-free until its natural end.

👉 How to avoid this in the future:

Try your best not to break execution while in a loop if you don’t need to. If you must press CTRL+BREAK during the loop, don’t instruct Excel to END execution. Instead, press Debug and in the line following the place execution has stopped, write some code to help the loop terminate safely.

🚫 And whatever you do, DON’T do what some forums advise, which is to add the following line to your code:

Application.EnableCancelKey = xlDisabled

This code will disable execution breaks in your code. So if you get caught in an endless loop, you won’t be able to use CTRL + BREAK to stop it. It’s not a good idea.

Previous
Previous

Book Review: Way of the Wolf

Next
Next

The Power of Making Mistakes