VBA Error – “code execution has been interrupted”

Edit (also in the comments section) –
Quite recently, I ran into this error again, and couldn’t quite seem to fix it with the EnableCancelKey thing.. I had mistyped ‘False’ for ‘xlDisabled’. But thanks to that, I found a new way (lot easier and quicker) to fix this thing –
Just press Ctrl + Break twice, and the Break mode is disengaged! Isn’t it something??

*************************************** Original Post ***************************************

Yet another bug! I’ve been quite heavily into VBA lately, and that’s one reason for such error posts! This has been quite a pain in the neck for VBA programmers, esp. if you haven’t quite hit upon this before.

This is how the story goes…

You’re heavily into the coding of your product, and suddenly, you figure out that you’ve entered an infinite loop. What do your instincts say?? Ofcourse! Ctrl+Break! Now, that was eass, wasn’t it… But what follows isn’t!

So, going forward, you actually go on making more tweaks in your code, and as you run it, you suddenly find an error (the title name). You try running it manually then; to your surprise, it runs quite fine. Again, when you run the whole code, the error pops up. To your frustration, this happens quite often within one piece of code! No Logical or Autorun Errors, and still… You finally turn to the one last resort – Google!

And the story ends thereafter ~

Here’s the reason why this happens. This is a so called poorly documented piece of information by Microsoft, but we users have taken care of that.

Once you press Crtl+Break, it trips off a mode that makes the compiler go to the break mode at random intervals. Nothing you do can stop this, unless you restart your machine. This is specially the case when you’ve got custom formulas, that run devoid of any programming.

Otherwise, you could easily put a fix to this in your program by using the following line at the start of your procedure:

Application.EnableCancelKey = xlDisabled

This helps reset the errorhanding at the end of the execution of your code. Which implies, you don’t need to worry about xlEnabled. But as I said, sometimes, it’s only a restart that can help you! (Don’t forget to save before you restart) Quite a pain in the ***! Especially if you are in the middle of something.

Info on EnableCancelKey

This property has three modes as listed below:

xlDisabled -> which ignores pressed keys and solves the problem
xlInterrupt -> which sets the error handling back to Excel’s control
xlErrorHandler -> wich makes error go to your error handling section

Thus when the code you are running is done, this property is set back to its default. Hence no need to worry about that.

A nice piece of information if ever you get caught in this.

Advertisements

4 thoughts on “VBA Error – “code execution has been interrupted”

  1. jennifer rosenow

    Can I just say that you are my new hero. I’ve been trying to figure this out for weeks! It was so frusterating after all of the work that’s been put into it. Thank you so much!

    Reply
  2. Arun Post author

    Reblogged this on One Stop Analytics and commented:

    Additions to this Post –
    Quite recently, I ran into this error again, and couldn’t quite seem to fix it with the EnableCancelKey thing.. I had mistyped ‘False’ for ‘xlDisabled’. But thanks to that, I found a new way (lot easier and quicker) to fix this thing –
    Just press Ctrl + Break twice, and the Break mode is disengaged! Isn’t it something??

    Reply
  3. Nette

    Found your post when I had code that kept being “interrupted”. The Ctrl + Break twice did it for me. Thank you.

    Reply
  4. Pingback: Fix Vba Error Code 13 Windows XP, Vista, 7, 8 [Solved]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s