vba 按 Esc 时停止 Excel 宏执行将不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6776919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:39:49  来源:igfitidea点击:

Stopping Excel Macro executution when pressing Esc won't work

excelvbaexcel-vbaterminate

提问by ptpaterson

I'm running excel 2007 on XP.

我在 XP 上运行 excel 2007。

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.

除了按 Esc 键之外,有没有办法阻止宏在执行过程中运行?通常,如果我认为我创建了一个无限循环或以其他方式搞砸了某些东西,我会点击转义并抛出错误但宏停止。

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.

这一次(我以前做过但还不错),我设置了一个 msgbox 以进行一些快速调试。结果它必须循环大约 6000 次,这意味着我必须“确定”6000 个消息框,这需要几分钟。我在运行之前没有保存(另一个错误)所以我无法打开任务管理器退出。

Is there another way to stop the execution of a macro in case I goof up like this again?

有没有另一种方法可以停止执行宏,以防我再次出现这种情况?

回答by Tomalak

Use CRTL+BREAKto suspend execution at any point. You will be put into break mode and can press F5to continue the execution or F8to execute the code step-by-step in the visual debugger.

使用CRTL+BREAK在任何时候暂停执行。您将进入中断模式,可以按F5继续执行或F8在可视化调试器中逐步执行代码。

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.

当然,这只适用于没有消息框打开的情况,所以如果你的 VBA 代码由于某种原因不断打开消息框,在正确的时刻按下键会变得有点棘手。

You can even edit most of the code while it is running.

您甚至可以在运行时编辑大部分代码。

Use Debug.Printto print out messages to the Immediate Windowin the VBA editor, that's way more convenient than MsgBox.

用于在 VBA 编辑器中将Debug.Print消息打印到立即窗口,这比MsgBox.

Use breakpointsor the Stopkeyword to automatically halt execution in interesting areas.

使用断点Stop关键字在感兴趣的区域自动停止执行。

You can use Debug.Assertto halt execution conditionally.

您可以使用有条件Debug.Assert停止执行

回答by VBANoob

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

CTRL + SCR LK(滚动锁定)对我有用。

回答by jainashish

Sometimes, the right set of keys (Pause, Breakor ScrLk)are not available on the keyboard (mostly happens with laptop users) and pressing Esc2, 3 or multiple times doesn't halt the macro too.

有时,正确的键组(PauseBreakScrLk)在键盘上不可用(大多数发生在笔记本电脑用户身上)并且按Esc2、3 或多次也不会停止宏。

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.

我也被卡住了,最终在 Windows 的辅助功能中找到了解决方案,之后我尝试了所有研究过的选项,其中 3 个在 3 个不同的场景中对我有用。

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.

步骤 #01:如果您的键盘没有特定键,请不要担心,然后按Win+从 Windows 实用程序打开“屏幕键盘” 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

步骤 #02:现在,尝试以下任何选项,根据您的系统架构,即操作系统和 Office 版本,它们肯定会起作用

  • Ctrl+ Pause
  • Ctrl+ ScrLk
  • Esc+ Esc(Press twice consecutively)
  • Ctrl+ Pause
  • Ctrl+ ScrLk
  • Esc+ Esc(连续按两次)

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 F5or F8to continue the debugging.

您将使用上述组合键进入中断模式,因为宏会立即暂停执行以完成当前任务。例如。如果它是从 web 上拉取数据,那么它会在执行任何下一个命令之前立即停止,但在拉取数据后,可以按F5F8继续调试。

回答by Lisa

You can stop a macro by pressing ctrl+ breakbut if you don't have the break keyyou could use this autohotkey(open source) code:

您可以按ctrl+停止宏,break如果您没有中断键,则可以使用此自动热键(开源)代码:

+ESC:: SendInput {CtrlBreak} return

+ESC:: SendInput {CtrlBreak} 返回

Pressing shift+ Escapewill be like pressing ctrl+ breakand thus will stop your macro.

shift+Escape就像按ctrl+ 一样break,因此会停止您的宏。

All the glory to this page

此页的所有荣耀

回答by Robert

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:

我也喜欢使用 MsgBox 进行调试,并且我不止一次遇到过同样的问题。现在我总是在弹出窗口中添加一个取消按钮,如果按下取消则退出宏。示例代码:

    If MsgBox("Debug message", vbOKCancel, "Debugging") = vbCancel Then Exit Sub

回答by dave

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...)

ESC 和 CTRL-BREAK 刚才对我不起作用。但是 CTRL-ESC 起作用了!?不知道为什么,但我想我会把它扔在那里,以防它对其他人有帮助。(我忘记了循环中的 i = i + 1 ......)

回答by AMR

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

您还可以尝试使用“Break”按钮或“sys rq”按钮按下“FN”或功能键 - 系统请求,因为这 - 必须同时按下,这会停止任何正在运行的宏

回答by Manuel

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.

我发现有时当我打开第二个 Excel 窗口并在第二个窗口上运行宏时,第一个窗口的执行就会停止。我不知道为什么它不能一直工作,但你可以试试。

回答by Yemi Adeoye

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.

在执行我的宏之前,我忘了用 MsgBox 注释掉一行。这意味着我必须单击确定超过十万次。ESC 键只是转义消息框,但不会停止宏的执行。连续按住 ESC 键几秒钟帮助我停止执行代码。

回答by Syed Mohammed

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').

我的笔记本电脑没有 Break 或 Scr Lock,所以我设法通过按Ctrl + Function + Right Shift (激活“暂停”)使其工作。