中断/中止 VBA 循环

发布于 2024-09-10 14:17:36 字数 138 浏览 6 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

み青杉依旧 2024-09-17 14:17:36

当 Excel 忙于执行宏时,它不会响应按钮。
这里有三个选项:

  1. 使用 Ctrl+Break 键(与按钮不同)
  2. 使宏更快(也许将 Application.ScreenUpdating 设置为
    False 会有帮助)
  3. 制作你的宏
    通过插入要慢得多
    内循环中的DoEvents。这
    这样,Excel就会对其中的按钮做出响应
    与此同时。此按钮将触发的宏只会将一个全局变量设置为 True(显然,您的内部循环应在每次迭代时检查此变量,如果为 True 则退出)。

When Excel is busy executing your macro, it won't respond to a button.
You have three options here:

  1. Use Ctrl+Break keys (as apposed to a button)
  2. Make your macro much faster (maybe setting Application.ScreenUpdating to
    False will help)
  3. Make your macro
    much slower by inserting a
    DoEvents in the inner loop. This
    way, 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's True).
流绪微梦 2024-09-17 14:17:36

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.

记忆消瘦 2024-09-17 14:17:36

除了其他答案之外,

假设您想破坏代码并仍然在代码中执行某些操作,您可以通过捕获中断并处理它来做到这一点

您可以使用 Err.Number 捕获错误号

具体来说,Err.Number = 18 指的是用户中断

Sub handleError()
On Error GoTo MyErrorHandler
 Application.EnableCancelKey = xlErrorHandler

 'Just an example to show something is running
 For i = 0 To 900000
 Range("A1") = i
 Next i

MyErrorHandler:
If Err.Number = 18 Then '18 =User interrupt
     MsgBox " You clicked Ctrl + Break "
     Exit Sub

End If
End Sub

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

Sub handleError()
On Error GoTo MyErrorHandler
 Application.EnableCancelKey = xlErrorHandler

 'Just an example to show something is running
 For i = 0 To 900000
 Range("A1") = i
 Next i

MyErrorHandler:
If Err.Number = 18 Then '18 =User interrupt
     MsgBox " You clicked Ctrl + Break "
     Exit Sub

End If
End Sub
-残月青衣踏尘吟 2024-09-17 14:17:36

不,按钮不会停止正在运行的宏。这是开发人员的事情,默认情况下控制权不应该掌握在用户手中。您可以尝试以下组合键来停止宏。

  • Ctrl + 暂停/中断
  • Ctrl + ScrLk
  • Esc + Esc< /kbd>(连续按两次)

有时,键盘上无法使用正确的按键组(PauseBreakScrLk)(主要发生在笔记本电脑用户中)并且按 Esc 2、3 次或多次也不会停止宏。

我也陷入了困境,最终在 Windows 的辅助功能中找到了解决方案,之后我尝试了所有研究过的选项,其中以上 3 个选项在 3 种不同的场景中为我工作。

步骤#01:如果您的键盘没有特定键,请不要担心,按 Win + U 从 Windows 实用程序中打开“屏幕键盘”

步骤#02:现在,尝试上述任何一个选项,它们肯定会起作用,具体取决于您的系统架构,即操作系统和 Office 版本

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

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.

  • Ctrl + Pause/Break
  • Ctrl + ScrLk
  • Esc + Esc (Press twice consecutively)

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.

自此以后,行同陌路 2024-09-17 14:17:36
  • 在循环内添加对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文