0% found this document useful (0 votes)
20 views

Interrupt A Macro: Command Button Dim

This document explains how to interrupt a macro in Excel by pressing Esc or Ctrl + Break. It provides an example of an infinite loop macro and how to disable the ability to interrupt a macro.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Interrupt A Macro: Command Button Dim

This document explains how to interrupt a macro in Excel by pressing Esc or Ctrl + Break. It provides an example of an infinite loop macro and how to disable the ability to interrupt a macro.

Uploaded by

Yamini Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Interrupt a Macro

 
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:
Dim x As Long
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).

Application.EnableCancelKey = xlDisabled

5. Although, reset 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:

Application.EnableCancelKey = xlInterrupt

Note: If Excel freezes and you cannot interrupt your macro anymore, press Ctrl + Alt + Delete and close Excel.

You might also like