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!

One thought on “Breaking infinite loops in VBA

  1. Found this and you in StackOverflow. Since I found your reporting and not his, and wouldn’t have wasted my time in MSDN, you indeed made the world a better place.

    Someone has to, to counter Microsoft doing the exact opposite everytime they change Office…and every single time they’ve done so since 2003 – no exaggeration.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.