Interrupt a Macro in Excel
You can interrupt a macro in Excel at any time by pressing Esc or Ctrl + Break.
Place a command button on your worksheet and add the following code lines:
x = 5
Do While x > 2
x = x + 1
Loop
1. Click the command button on the sheet. This macro never stops because the part after 'Do While' will always be true (x will always be higher than 2).
2. To halt this infinite loop, press Esc or Ctrl + Break. The following dialog box will appear:
3. Click End to end the macro, click Debug to take a look at the macro in the Visual Basic Editor.
4. Add the following code line at the start of your code if you don't want users of your program to be able to interrupt your macro (not recommended).
5. Although Excel VBA resets the EnableCancelKey property automatically to xlInterrupt at the end of your macro, it's good practice (when using the previous code line) to end your macro with the following code line:
Note: if Excel freezes and you cannot interrupt your macro anymore, press Ctrl + Alt + Delete and close Excel.
If you're new here, welcome to Excel Easy! Join over 1 million monthly Excel learners. You can find popular courses here: Data Analysis in Excel and Excel VBA.