vba 如何自动停止VBA宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26368883/
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
How to stop VBA macro automatically?
提问by iliketocode
I know you can manually stop a running VBA macro with Ctrl+Break, but is there any way to have the code stop automatically if a certain condition is met? exit function/ exit subare not working, because they are only terminating the method they are called within.
我知道您可以使用Ctrl+手动停止正在运行的 VBA 宏Break,但是如果满足某个条件,有没有办法让代码自动停止? exit function/exit sub不起作用,因为它们只是终止它们在其中调用的方法。
For example,
例如,
sub a
call b
msgbox "a complete"
end sub
sub b
call c
msgbox "b complete" 'this msgbox will still show after the `exit sub` in 'c'
end sub
sub c
msgbox "entering c"
exit sub 'this will only `exit sub` 'c', but not 'a' or 'b'
msgbox "exiting c"
end sub
'OUTPUT:
'entering c
'b complete
'a complete
I suppose I could turn these sub's into function's and utilize return codes to know if the method executed successfully, but is there a simpler way to do this?
我想我可以将这些sub's 变成function's 并利用返回码来知道该方法是否成功执行,但是有没有更简单的方法来做到这一点?
回答by iliketocode
You can raise your own user-defined error with err.raise. This is similar to your example, but a little more powerful in that it is an actual error that will halt code execution, even if it's applied to a nested call.
您可以使用err.raise. 这与您的示例类似,但功能更强大,因为它是一个实际错误,会停止代码执行,即使它应用于嵌套调用。
For example,
例如,
sub a
on error goto exitCode
call b
msgbox "a complete"
exit sub 'you need this to prevent the error handling code from always running
exitCode:
msgbox "code is exiting..."
'clean up code here
end sub
sub b
call c
msgbox "b complete"
end sub
sub c
msgbox "entering c"
err.raise 555, "foo", "an error occurred"
msgbox "exiting c"
end sub
'OUTPUT:
'entering c
'code is exiting...
The err.raiseline will send the control to the exitCode:label even though it was called outside of a. You could use any conditions to test if this custom error should be thrown.
该err.raise行会将控件发送到exitCode:标签,即使它是在a. 您可以使用任何条件来测试是否应该抛出此自定义错误。
More on the errobject and VBA error handling-
有关err对象和 VBA 错误处理的更多信息-
https://msdn.microsoft.com/en-us/library/ka13cy19(v=vs.90).aspx
https://msdn.microsoft.com/en-us/library/ka13cy19(v=vs.90).aspx
回答by Ceres
I believe want you want to do is raise the error again in your error handling code. Try something like this (not tested):
我相信您想要做的是在错误处理代码中再次引发错误。尝试这样的事情(未测试):
Private Sub Test
On Error Goto bad
x = 0
debug.print 1/x
Exit Sub
bad:
'Clean up code
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End Sub
When using On Error Goto, you need an Exit Subbefore the label. Otherwise your code will fall through to the error handler even when there is no error.
使用时On Error Goto,您需要Exit Sub在标签之前添加一个。否则,即使没有错误,您的代码也会落入错误处理程序。
Your first error handling doesn't catch actual errors that might occur during runtime other than what you are testing for. Also, there is not a convenient way to signal the calling function that something went wrong unless you add checks in all the calling routines.
您的第一个错误处理不会捕获运行时可能发生的实际错误,而不是您正在测试的错误。此外,除非您在所有调用例程中添加检查,否则没有一种方便的方法向调用函数发出出错信号。
Note that it's generally considered bad design though to raise an error only for flow control:
请注意,尽管仅针对流量控制引发错误,但通常被认为是糟糕的设计:
Private Sub Test
If x = 0 Then
Err.Raise
End If
End Sub
回答by meMadhav
You can create Macro to perform below steps/operation and call whenever your code meets certain condition to abort the other running macros:
您可以创建宏来执行以下步骤/操作,并在您的代码满足特定条件时调用以中止其他正在运行的宏:
Step 1-- Menu bar -> Run -> Reset
步骤 1-- 菜单栏 -> 运行 -> 重置
Step 2-- Menu bar -> Run -> Break
步骤 2-- 菜单栏 -> 运行 -> 中断

