Breaking infinite loops in VBA

I do a lot of Visual Basic for Applications (VBA) development in Microsoft Office. Any program can get stuck in an infinite loop, and when my VBA programs have it has always meant data loss. The Internet to the rescue, as of not quite a month ago!

To break VBA code execution, at least in Office 2013, hit Ctrl+Scroll Lock.  Obvious, right? 😉

Huge thanks to MSDN user povidla, who posted this solution in the MSDN forums.  Povidla, if you’re reading this, please comment so I can thank you directly!

Edit I have found that, in some situations, this doesn’t work unless there is a DoEvents call to catch the Ctrl+ScrLk.  If you have a loop that might be infinite, sticking a DoEvents in it is probably a good practice.

Greetings to Stack Overflow users!  I keep hoping that Povidla will find this page.  If you happen to know who Povidla is, please pass along a thank you from me.  Regardless, thanks for reading!

Advertisements

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