Stopping Excel Macro executution when pressing Esc won't work - vba

I'm running excel 2007 on XP.
Is there a way to stop a macro from running during its execution other than pressing escape? Usually if I think I created an infinate loop or otherwise messed something up I hit escape and it throws an error but the macro stops.
This time (and I've done it before but not this badly), I set up a msgbox for some quick debugging. Turns out it had to loop about 6000 times, which made means I had to "OK" 6000 message boxes, which took several minutes. I didn't save before running (another mistake) so I couldn't open task manager to exit.
Is there another way to stop the execution of a macro in case I goof up like this again?

Use CRTL+BREAK to suspend execution at any point. You will be put into break mode and can press F5 to continue the execution or F8 to execute the code step-by-step in the visual debugger.
Of course this only works when there is no message box open, so if your VBA code constantly opens message boxes for some reason it will become a little tricky to press the keys at the right moment.
You can even edit most of the code while it is running.
Use Debug.Print to print out messages to the Immediate Window in the VBA editor, that's way more convenient than MsgBox.
Use breakpoints or the Stop keyword to automatically halt execution in interesting areas.
You can use Debug.Assert to halt execution conditionally.

CTRL + SCR LK (Scroll Lock) worked for me.

Sometimes, the right set of keys (Pause, Break or ScrLk) are not available on the keyboard (mostly happens with laptop users) and pressing Esc 2, 3 or multiple times doesn't halt the macro too.
I got stuck too and eventually found the solution in accessibility feature of Windows after which I tried all the researched options and 3 of them worked for me in 3 different scenarios.
Step #01: If your keyboard does not have a specific key, please do not worry and open the 'OnScreen Keyboard' from Windows Utilities by pressing Win + U.
Step #02: Now, try any of the below option and of them will definitely work depending on your system architecture i.e. OS and Office version
Ctrl + Pause
Ctrl + ScrLk
Esc + Esc (Press twice consecutively)
You will be put into break mode using the above key combinations as the macro suspends execution immediately finishing the current task. For eg. if it is pulling the data from web then it will halt immediately before execting any next command but after pulling the data, following which one can press F5 or F8 to continue the debugging.

You can stop a macro by pressing ctrl + break but if you don't have the break key you could use this autohotkey (open source) code:
+ESC:: SendInput
{CtrlBreak}
return
Pressing shift + Escape will be like pressing ctrl + break and thus will stop your macro.
All the glory to this page

I also like to use MsgBox for debugging, and I've run into this same issue more than once. Now I always add a Cancel button to the popup, and exit the macro if Cancel is pressed. Example code:
If MsgBox("Debug message", vbOKCancel, "Debugging") = vbCancel Then Exit Sub

You can also try pressing the "FN" or function key with the button "Break" or with the button "sys rq" - system request as this - must be pressed together and this stops any running macro

I've found that sometimes whem I open a second Excel window and run a macro on that second window, the execution of the first one stops. I don't know why it doesn't work all the time, but you may try.

ESC and CTRL-BREAK did not work for me just now.
But CTRL-ESC worked!?
No idea why, but I thought I would throw it out there in case it helps someone else.
(I had forgotten i = i + 1 in my loop...)

Just Keep pressing ESC key. It will stop the VBA. This methods works when you get infinite MsgBox s

I forgot to comment out a line with a MsgBox before executing my macro. Meaning I'd have to click OK over a hundred thousand times. The ESC key was just escaping the message box but not stopping the execution of the macro. Holding the ESC key continuously for a few seconds helped me stop the execution of the code.

My laptop did not have Break nor Scr Lock, so I somehow managed to make it work by pressing Ctrl + Function + Right Shift (to activate 'pause').

Related

SAP GUI scripting - Button press fails

I am using a macro in an Excel file that runs the SAP GUI. There is a step where, when I click a button in SAP there will be another window that pops up.
For that I have written a code like this:
session.findbyid("wnd[0]/XX/btnXX").press
session.findbyid("wnd[1]/XX/btnXXX").press
There is a button (btnXXX) in the window (wnd[1]). But when I execute this query, I am getting an error object not found for findbyid.
When I keep the break point and execute it, it is throwing error on 2nd line in the above code. I try to pick the activewindow.name and it shows wnd[0] still. Here the issue is wnd[1] is not getting opened.
Does somebody know why the 2nd "button press" doesn't work?
You should be able to replace all mouse clicks with keyboard strokes.
Replace:
session.findbyid("wnd[0]/XX/btnXX").press
With:
session.findById("wnd[0]").sendVKey(N)
Where N is the linked hot-key ID.
To get the exact command, use SAP script recording and only use the keyboard to transition between views and windows. The easiest way to determine how is to hover your mouse over the buttons you would normally click to learn the hot-key then record the hot-key.
Note 1) So far I have found that btn[XX] always maps to sendVKey(XX), but I can't be certain this is always the case.
Note 2) sendVKey always appears to be referenced off the window (wnd[Y]) even if a button is another layer down (/tbar, /usr, etc.).

Intellij macros stop working after a couple of times

I'm working on IntellijUltimate-2019.2 but have seen this issue on previous versions as well. When I try to create a macro and replay it, it works fine one or two times, but starts doing something else next time onward.
I do have some custom shortcuts setup and I'm running Intellij over RDP. My local system is Ubuntu and RDPing using Remmina.
Has anyone faced similar issues before or have any suggestions on how to fix this?
A silly one, but posting a response here (instead of deleting this post) in case anyone else is running into the same issue.
If you are using a combination of keys to trigger a macro (e.g. Alt, Ctrl or Shift), make sure to leave the first key immediately after pressing the second key, else they will interfere the macro replay.
I was using Alt + J to trigger the macro. The macro included typing "P", but because I wasn't letting go Alt fast enough, instead of typing "P", it was triggering another action that I had mapped against Alt + P.

Control Left doesn't move the cursor to the previous word

In no program that is capable of editing text (such as notepad, Notepad++, or Chrome) on either my Windows Vista or Windows 8 laptops does CTRL+LEFT move the cursor to the previous word.
Prior Troubleshooting Steps (other than researching):
CTRL+RIGHT moves the cursor to the next word.
CTRL+SHIFT+LEFT highlights the previous word.
I put the following line in AutoHotKey (and saved/reloaded the script) and CTRL+LEFT then moves the cursor to the next word (as it should):
^Left:: Send ^{Right}
As step 3 above, but the following line did nothing when I used ALT+LEFT.
!Left:: Send ^{Left}
I'm at a loss. I run Avast Antivirus, Spybot, and ZoneAlarm Firewall on strict settings and scans aren't turning up anything. Rebooting in safe mode has no effect either.
Are you running any other AutoHotkey scripts on these computers that have Ctrl or Left Arrow set as hotkeys?
If so and you are not using #IfWinActive or ~ then it's possible you are blocking yourself by having those keys Hooked.
http://www.autohotkey.com/docs/FAQ.htm
http://www.autohotkey.com/docs/Hotkeys.htm#Symbols
You can also try various methods of Sending those keys, SendRaw, SendInput, SendEvent etc... More details here:
http://www.autohotkey.com/docs/commands/Send.htm
To verify that the keys are being read by your script...
Run a script with:
#InstallKeybdHook
~LCtrl & Left::
Send {RCtrl Down}{Left Down}{RCtrl Up}{Left Up}
Double Left Click AutoHotkey Script Tray Icon->Select View->Key history
Press Ctrl + Left and verify that your keys are being read.
If all the above has be tried, it is possible that some software on your system has Hooked your Keys and you will have to figure out what you installed recently and remove that program. Good luck.

TextMate: Keyboard shortcuts sometimes don't work, then work again after restart

I moved to Lion 2 months ago, and since then I sometimes encounter that a TextMate keyboard shortcut doesn't work anymore (which means: nothing happens), and then, after a restart of TextMate, it works again.
This time, it was CTRL + ALT + SHIFT + DOWN, which should trigger Cucumber's Go to current step command. After restart of TM, it worked again. I also experienced this with CMD+SHIFT+7 (which results in Cmd-/ on my swiss layout) for commenting out lines.
Any idea about what's the problem? As I mentioned in a previous post, for the commenting out shortcut it finally happened to be the application Skitch sometimes which blocks this commend and prevents it from being sent to TM. But this doesn't have to do with the restart scenario I have now.
Anyway, it would be nice to have OSX display some info box whenever a keyboard shortcut is pressed which tells what exactly was executed, so e.g. when pressing CMD+S it would tell "Save Document", or CMD+SHIFT+3 "Take Screenshot", etc., so you'd always know what was triggered and don't have to think a shortcut ended up in Nirvana... Maybe there is some application or OSX extensions that does this?
I've found that when this happens, there's a background process going on that is, for some reason, disabling some or all of your keyboard shortcuts. I also found that it's very easily remedied by typing Command-. (period) which tells TextMate to cancel the running background task.
Try it next time it happens and see if that doesn't at least ameliorate the problem. Fixing it would require figuring out what background task is getting wedged and either fixing or eliminating it.

excel VBA break execution when there's no break key on keyboard [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I'm just noticing that on my laptop (Dell XPS 15z) there's no BREAK key (no dedicated number keypad). I'm running the debugger step-by-step and then when all seems fine, I just let it play out. However, it's running in an infinite loop and now I'm realizing there's no way to stop it without the break key!!!!
Is this a bad joke? I had to reboot the computer; is there a more graceful way??
just press the escape key twice ;-)
Solution #1 (It works most of the time):
When some particular keys (Pause, Break or ScrLk) are missing on the keyboard and pressing Esc 2, 3 or multiple times doesn't stop the macro too.
Step #01: If your keyboard does not have a specific key, please do not worry and open the 'OnScreen Keyboard' from Windows Utilities by pressing Win + U.
Step #02: Now, try any of the below option and of them will definitely work depending on your system architecture i.e. OS and Office version
Ctrl + Pause
Ctrl + ScrLk
Esc + Esc (Press twice consecutively)
You will be put into break mode using the above key combinations as the macro suspends execution immediately finishing the current task. For eg. if it is pulling the data from the web then it will halt immediately before executing any next command but after pulling the data, following which one can press F5 or F8 to continue the debugging.
When Solution #1 doesn't work or you need a permanent solution then try adding a new KEY to the Registry. (I am running Win 10 Creators Update on Dell 3530 Inspiron Laptop)
Disclaimer: Please only make changes to Windows Registry at your own risk and it is advisable only if you are windows administrator or have prior experience in the changing the key values in the registry.
You can also look for Solution #2 which use accessibility keyboard Win + U
Solution #2:
1) Click the START, then type REGEDIT into the search box. This will start the registry editor.
2) Explore the Registry, expanding each element
HKEY_LOCAL_MACHINE
-SYSTEM
-Current Control Set
- Control
- Keyboard Layout
Click on the Scancode Map, and EDIT the key values to match that shown below.
i.e. to have values of : 00,00,00,00,00,00,00,00,02,00,00,00,46,e0,44,00,00,00,00,00
Once Done you will need to reboot your laptop, but after that, you can use as the pause/Break Key.
To understand it in detail and customize it as per your needs, please refer this link: KeyBoard Scancodes
I have a Dell Precision without a Pause/Break key but there is a replacement for that: Fn + B, so:
- Fn + B == Pause (pause execution);
- Ctrl + Fn + B == Break (break execution);
- Win + Fn + B == Windows + Pause (open System properties).
You can execute the command:
dir /s C:\
and test the Pause and Break combination keys while dir lists all files in drive C.
reference
Despite this being an old question, I have just encountered the same issue and discovered that Ctrl+Scroll lock works as a break.
"Scroll lock" can be accessed on my DELL Laptop via the blue function key "Fn" and the "F6" key which has a small blue lock symbol on it with an ⇳ symbol in it. At other computers it may look like ⤓ . So effectively I press Ctrl+Fn+F6.
I have an HP laptop and this works when I want to break after a msgbox, hold fn+ctrl and click right shift key (has pause on it as well) then respond to msgbox and code will break.
On my Dell at least : ctrl + alt + esc BEFORE your cursor starts looping itself
I got this info from aidangrogan on this website :
http://en.community.dell.com/support-forums/laptop/f/3518/t/19355018
So I don't claim to have found it myself. All credits to him (or her)
Hold CTRL+Fn & hit Shift - Stopped my macro from running. I don't have a 'Break' key either on the keyboard of my HP Pavilion G7.
Ctrl + Fn + B works for Excel 365 (Office 365) on my Dell Latitude 5480 (no Break key).
I found this thread and saw the solution that has the most votes, but then realized my laptop has no Pause, Break or ScrLk) keys. I tried other proposed solutions, like 'OnScreen Keyboard' or alternative key combinations, but those didn't work either.
Then it hit me: if it's a phantom break point associated with particular line(s) of code, why not just get rid of the line(s)?
So that's what I did:
I cut the line(s) where execution was breaking.
Ran the macro again (it didn't break this time)
Pasted the line(s) again.
It worked for me, when no other key combination (onscreen or physical) worked. Hope it helps someone!
None of the above answers work for me.
I did how ever find this, it's not perfect since you need to keep ALT pressed and then a keys twice. On top of that it's depended on what language your Excel is set to.
In an English Excel you'd use ALT + R + R in German you need to use ALT + U + R
I have a Japanese Layout keyboard. I neither have a scroll lock nor a break on my laptop-Lenovo E470.
A long press on the Esc key worked as a terminator for me.
I found a way round this, opened another instance of Excel and another spreadsheet, then opened the VBA editor from there and it showed as "running" so I could click the stop button
Because all of the above werent working for my application, I ended up writing a VBS script which I can run from the terminal. Not elegant. Probably not the first choice if one of the others work. But, effective.
break.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%{BREAK}"