在 2010 (2013) Excel VBA 中控制跳出无限循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21737796/
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 17:46:00  来源:igfitidea点击:

Control Break out of Infinite Loop In 2010 (2013) Excel VBA

excelvbainfinite-loopbreakctrl

提问by Tommy Z

If I write code creating an infinite loop, with my new Excel, the Ctrl + Break no longer works. Neither does the Esc key, etc.

如果我使用新 Excel 编写创建无限循环的代码,则 Ctrl + Break 不再有效。Esc 键等也不行。

I've looked all over the web and it appears that Microsoft has a bug and doesn't care to fix it.

我查看了整个网络,似乎 Microsoft 有一个错误并且不想修复它。

Is there a way to re-introduce the Ctrl + Break function to VBA so if this happens in the future I don't lose work / force close?

有没有办法将 Ctrl + Break 功能重新引入 VBA,所以如果将来发生这种情况,我不会失去工作/强制关闭?

回答by John

Alt + Esc. Hold down the keys until it breaks.

Alt+Esc。按住按键直到它断裂。

From Windows 7 on, this will cycle through all open windows. Pay no mind, just keep squeezing it.

从 Windows 7 开始,这将在所有打开的窗口中循环。别介意,继续挤压它。

回答by cxw

On Office 2013, at least, Ctrl+Scroll Lock does it. I didn't need to change any settings or enable anything.

至少在 Office 2013 上,Ctrl+Scroll Lock 可以做到这一点。我不需要更改任何设置或启用任何东西。

(Blatant plug: my blog postlinking to the original source of this info :) )

(公然插件:我的博客文章链接到此信息的原始来源:))

回答by washicd

For those who tried the previously proposed solutions to no avail, try this. It worked for me (windows8, Excel 2016)

对于那些尝试过之前提出的解决方案无济于事的人,试试这个。它对我有用(windows8,Excel 2016)

On the VBA code window, left-click and hold (as though you are going to drag that window) while holding down the Alt+ESC keys. It broke the execution and asked me if I wanted to continue, or debug... I of course chose debug.

在 VBA 代码窗口中,按住 Alt+ESC 键的同时左键单击并按住(就像您要拖动该窗口一样)。它中断了执行并问我是要继续,还是要调试……我当然选择了调试。

回答by chris neilsen

One way to mitigate this very annoying behaviour is to include

减轻这种非常烦人的行为的一种方法是包括

DoEvents

In the loop. It doesn't have to be executed every iteration, so long as it is called periodically, Ctrl Break will still work.

在循环。不必每次迭代都执行,只要定期调用,Ctrl Break 仍然有效。

Note: I usually remove this after the code is debugged, to avoid any performance impact

注意:我通常在代码调试后删除它,以避免任何性能影响

回答by user7076157

If it can help anyone, pressing Ctrl+break when the macro is not running only works if you do it outside of the developer.

如果它可以帮助任何人,则在宏未运行时按 Ctrl+break 仅适用于在开发人员之外执行的操作。

回答by Alex

An alternative will be introduce error intentionally in the loop. Say for e.g. your loop should not be running more than 1000 times:

另一种方法是在循环中故意引入错误。比如说你的循环不应该运行超过 1000 次:

Function XYZ()
do while(..)
errcnt = errcnt + 1

if errcnt > 1000 then
  cells.find(what:="Chunk Norris").activate
exit function
endif

loop
end function

it will produce an error and 'break' the code

它会产生错误并“破坏”代码

回答by Sebastiaan35

Break (Ctrl + Break) is also not available in the VBA editor if you are in design mode. In that case you can press the Design Mode icon which is to the right of the square stop icon. This brings the editor back in regular mode and the break option then becomes available.

如果您处于设计模式,则中断 (Ctrl + Break) 在 VBA 编辑器中也不可用。在这种情况下,您可以按方形停止图标右侧的设计模式图标。这会将编辑器带回常规模式,然后中断选项可用。