Forcing a break in VBA if stuck in a continuous loop

JetEscamilla

Board Regular
Joined
Apr 17, 2006
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
I think the title is self explanatory. I have a macro running which I was doing step by step using F8. The sand dial is on my cursor because the macro is just running away. How can I force it to stop. Idiot me did not save before running and I dont want to lose what I just wrote.
 
What if you don't have a pause/break key on your computer?

Yeah, I wondered about this too but I found a solution!

Press the start key (or click the start button) and type "ON". You should see a program called "On-Screen Keyboard". Click on this.

First, bring the VBA editor into focus, then bring the On-Screen Keyboard into focus. Click "CTRL" then click "PAUSE" on the on-screen keyboard. This will break your loop (if you have DoEvents in the loop).
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Thanks for the responses

ctrl+break is not working for me. The sand dial stays on my cursor and will not let me click anywhere on the VBA window. It may be that excel just bugged out on me and not stuck in a loop. Any other ideals.

Same here...

However, Ctrl-Alt-Del is a bad and brutal way to kill it.

What works for me on my current system is:

Ctrl-Shift-Break

That brings up the normal "Code execution has been interrupted" dialog box.

Funny, now that I have got out, both Ctrl-Break and Ctrl-Shift-Break are working for me. Maybe I hit Pause first, the first time???
 
Upvote 0
Hi ChaosFreak.

Yeah, I wondered about this too but I found a solution!

Press the start key (or click the start button) and type "ON". You should see a program called "On-Screen Keyboard". Click on this.

First, bring the VBA editor into focus, then bring the On-Screen Keyboard into focus. Click "CTRL" then click "PAUSE" on the on-screen keyboard. This will break your loop (if you have DoEvents in the loop).

Which O/S are you running? This doesn't work on my XP machine. (If Microsoft thinks the world is gonna stop using XP after "April 8th, 2014" they could be in for another bad year...)

Anyway, on my system [Start] "ON" opens Notepad. I guess it interprets 'O' as Open, and Notepad is the first item with N as its shortcut letter, showing on the first panel of the Start menu...
 
Upvote 0
Which O/S are you running? This doesn't work on my XP machine. (If Microsoft thinks the world is gonna stop using XP after "April 8th, 2014" they could be in for another bad year...)

Anyway, on my system [Start] "ON" opens Notepad. I guess it interprets 'O' as Open, and Notepad is the first item with N as its shortcut letter, showing on the first panel of the Start menu...

Hehe, yeah...I'm running Windows 8.1. Starting with Vista, if you press the start key and start typing the name of a program, it searches for all programs that match the string you've entered. Also, the On-Screen Keyboard is only available in Windows 8.
 
Last edited:
Upvote 0
I program for infinite loops such as:

if x > 100 then
end
end if

But now I'm thinking that maybe there is a bit of code similar to 'end' which is called 'break'. That way I could look at the locals window and find out what is wrong. Does this function exist? I looked for it on the web and couldn't find it.
 
Upvote 0
I program for infinite loops such as:

if x > 100 then
end
end if

But now I'm thinking that maybe there is a bit of code similar to 'end' which is called 'break'. That way I could look at the locals window and find out what is wrong. Does this function exist? I looked for it on the web and couldn't find it.


Exit xxx is roughly equivalent to Break.

are you in a for ... next loop? Try

Exit For

are you in a sub? Try

Exit Sub

are you in a function? Try

Exit Function

etc....
 
Upvote 0
During testing only (you should remove it once your code is proofed as being okay), try including a DoEvents statement as the first statement after the loop's starting line... it will slow the loop up, but should allow the Ctrl+Break or Ctrl+Shift+Break to always stop the loop at any point.
 
Upvote 0
I often work via a remote desktop application. If I could do [Ctrl+Break] or something like that, I would stop the remote desktop while the Excel VBA loop continued. A forced shutdown ( equiv to [Ctrl+Alt+Del] is still possible, but brutal, as betaVBA noted ).

What works best for me is opening the task manager (Right click on the task bar which runs along the lower edge of the screen, and choose Task Manager), find Excel, highlight it, and kill it.
 
Upvote 0
Old thread I know,

Instead of "END" put "STOP" in the loop, eg

do
..
.
.
Count = Count +1
If Count >= 100 then
Stop
Count = 0
End If
.
.
Loop

This way you can press F5 to continue, or press "Reset" in the "Run" tab at the top of the VBA editor screen
 
Upvote 0
Hi,

in order to stop a vba programm either running in Excel or Word you have to press Ctrl+Break, but there are some keyboards like Dell which doesn't send the correct key codes for stopping. So, if you have a keyboard which doesnot support the break code, Ctrl+break is not going to work.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top