VBA - Escape Key and Error Handler

vba_monkey

Board Regular
Joined
Dec 18, 2013
Messages
112
Hello,

I am trying to get excel to go to my error handler if Esc is pressed whilst the macro is running. This is what I have so far but when I press Esc, Excel goes to the error handler and breaks on the first line with error 'User interrupt occurred' instead of running through the code in the error handler:

Code:
On Error GoTo Error_Handler

Application.EnableCancelKey = xlErrorHandler

Error_Handler:
If Err = 18 Then
    MsgBox "Macro exited...", vbExclamation, "Stopped!"
Else
    Debug.Print "Line: " & Erl & "  Desc: " & Err.Description
    MsgBox "An error occurred, exiting macro...", vbCritical, "Error!"
End If
End Sub

Any ideas please?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

works ok for me. I copied and pasted as is in case there were typos. I get the MsgBox 'Macro exited... when pressing Esc.
I use the Application.EnableCancelKey right at the start of the sub and the Error_Handler code is the last block before End Sub.
 
Upvote 0
I have it set out the same but still not working. I have tried it in another workbook and it does work so it must be workbook specific. Can anyone think of what may be stopping it from working?
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top