中断/中止 VBA 循环
在 Excel 上的 VBA 中,我对数千个单元格进行了循环,这需要几分钟的时间。
是否可以中止长期循环(如果可以,如何)/我可以构建一个按钮或类似的东西来手动中断此循环吗?
构建一个按钮并用宏覆盖它不是问题,只有代码本身有问题。
In VBA on Excel, I have a loop over several thousands of cells, which takes some minutes.
Is it possible to abort a long term loop (if so, how) / can I build a button or something like that to interrupt this loop manually?
Building a button and overlaying it with a macro is not a problem, only the code itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当 Excel 忙于执行宏时,它不会响应按钮。
这里有三个选项:
Ctrl+Break
键(与按钮不同)Application.ScreenUpdating
设置为False
会有帮助)通过插入要慢得多
内循环中的
DoEvents
。这这样,Excel就会对其中的按钮做出响应
与此同时。此按钮将触发的宏只会将一个全局变量设置为 True(显然,您的内部循环应在每次迭代时检查此变量,如果为 True 则退出)。
When Excel is busy executing your macro, it won't respond to a button.
You have three options here:
Ctrl+Break
keys (as apposed to a button)Application.ScreenUpdating
toFalse
will help)much slower by inserting a
DoEvents
in the inner loop. Thisway, Excel will resond to buttons in
the meantime. The macro this button would trigger would just set a global variable to
True
(obviously, your inner loop should check this variable on each iteration, and exit if it'sTrue
).Application.EnableCancelKey
可能就是您正在寻找的东西。请参阅链接 url 上的示例代码,了解当用户按下转义键时将控件传递给错误处理程序。
Application.EnableCancelKey
could be the thing, you are looking for.See the example code on the linked url, on passing the control to error handler when user presses escapes.
除了其他答案之外,
假设您想破坏代码并仍然在代码中执行某些操作,您可以通过捕获中断并处理它来做到这一点
您可以使用 Err.Number 捕获错误号
具体来说,Err.Number = 18 指的是用户中断
In addition to other answers,
Lets say you want to break the code and still perform certain actions in your code, You can do so by capturing the interrupt and handling it
You can use Err.Number to capture the error number
Specifically, Err.Number = 18 refers to user breaks
不,按钮不会停止正在运行的宏。这是开发人员的事情,默认情况下控制权不应该掌握在用户手中。您可以尝试以下组合键来停止宏。
有时,键盘上无法使用正确的按键组(Pause、Break 或 ScrLk)(主要发生在笔记本电脑用户中)并且按 Esc 2、3 次或多次也不会停止宏。
我也陷入了困境,最终在 Windows 的辅助功能中找到了解决方案,之后我尝试了所有研究过的选项,其中以上 3 个选项在 3 种不同的场景中为我工作。
步骤#01:如果您的键盘没有特定键,请不要担心,按 Win + U 从 Windows 实用程序中打开“屏幕键盘” 。
步骤#02:现在,尝试上述任何一个选项,它们肯定会起作用,具体取决于您的系统架构,即操作系统和 Office 版本
您将使用上述组合键作为宏进入中断模式完成当前任务后立即暂停执行。例如,如果它正在从网络中提取数据,那么它将在执行任何下一个命令之前但在提取数据之后立即停止,然后可以按 F5 或 F8 继续调试。
No, a button won't stop the running macro. It's a developer thing and control shouldn't be in user's hands by default. You can try the below combination of keys to stop the macro.
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 above 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 above option and of them will definitely work depending on your system architecture i.e. OS and Office version
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.
在循环内添加对 DoEvents 的调用。
添加公共布尔模块级别标志变量
向 UI 添加按钮
在按钮的事件处理程序中将标志设置为 true
在循环中检查变量,如果为 true 则中止循环。
我已在 Excel 用户窗体中使用此功能,但未在工作表上的按钮中使用它。
Add a call to DoEvents inside the loop.
Add a public boolean module level flag variable
Add a button to the UI
In the event handler for the button set the flag true
in the loop check the variable, if it is true abort the loop.
I've used this in an Excel userform but not with buttons on a worksheet.