VBA - Escape Key and Error Handler

  • 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:

    Any ideas please?

    Also posted here:

  • Re: VBA - Escape Key and Error Handler

    Thanks, I've already seen that but it doesnt really answer why my code isnt working as it says it should in that article. I have a Go To statement and EnableCancelKey is set to xlErrorHandler. If Esc is pressed then it goes tp the error handler as it should but then seems to return to xlInterrupt without running through the code in the error handler.

  • Re: VBA - Escape Key and Error Handler

    This worked fine for me

    Pressing esc key gives the first error message.


    y = x


    y = x / 0

    gave the second message.

    Replacing the For.......Next loop with your actual code should make it work OK with your actual code.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA - Escape Key and Error Handler

    I can get it to work too on a blank sheet so there's obviously something going wrong in my workbook. Here's the full code, I'm not sure you'll be able to test as it involves updating pivot tables that are connect to a network data source.

    I thought it might be this part that is doing it but commenting them out has no effect:

    Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Application.Calculation = xlCalculationManual

    Full code:

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!