Search code examples
excelvbadebugginginfinite-loophalting-problem

How to escape infinite loop in VBA / VB.NET


bit of a stupid question, but I couldn't find any answers to it.
Sadly, I made a very shameful error, where I created an endless loop by accident.

Private Sub Textbox1_Change()
    Do While Len(Trim(Textbox1.Text)) > 4
       MsgBox "Please enter your birthyear in format of ####"
    Loop
End Sub

As I wanted to enforce users to type only 4 digits, obviously not realising I made an given it's impossible to close the MsgBox as once you reach >4 characters, it keeps on creating new MsgBoxes even after you QueryClose/OK the one that popped up.

enter image description here

Is there a way I could cancel it, without closing Excel altogether? Sadly, I can't even pause the code given the MsgBox opened in modal form and I can't click any of the editor elements.

enter image description here


Solution

  • Just press Ctrl + Alt + Pause/Break.

    This will interrupt your routine.

    The Pause/Break button is standardly located above the PageUp button on most keyboards