如何将引发的错误传递给 VBA 中的自定义错误处理程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14783616/
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 do I pass a raised error to a custom error handler in VBA?
提问by Lokerim
I've been using VBA in Excel for a while, and I use a custom error handler for all of my procedures. I find myself, for the first time, in the position of needing to useErr.Raise
(to deal with a Case Else
situation in a Select Case
block)., and I can't figure out how pass the error to the custom error handler. Instead of passing the raised error to the custom handler, VBA pops up its own ugly and fairly useless error dialog. If anyone can tell me a way to get around this I'd be very appreciative.
我在 Excel 中使用 VBA 已经有一段时间了,我对所有过程都使用了自定义错误处理程序。我第一次发现自己处于需要使用的位置Err.Raise
(处理块中的Case Else
情况Select Case
)。我不知道如何将错误传递给自定义错误处理程序。VBA 没有将引发的错误传递给自定义处理程序,而是弹出其自己丑陋且相当无用的错误对话框。如果有人能告诉我解决这个问题的方法,我将不胜感激。
Below is a genericized version of the code I'm using (function/variable names have been changed to protect the innocent). The gErrorHandler
object is a globally dimensioned class module variable which handles errors from any and all procedures.
下面是我正在使用的代码的通用版本(函数/变量名称已更改以保护无辜者)。该gErrorHandler
对象是一个全局维度的类模块变量,它处理来自任何和所有过程的错误。
Public Function MyFunction(dblInputParameter As Double) As Double
On Error GoTo Err_MyFunction
Dim dblResult as Double
Select Case dblInputParameter
...Several case statements go here...
Case Else
Err.Raise vbObjectError + 1000, "MyProjectName.MyObjectName", "Error Description"
End Select
MyFunction = dblResult
Exit_MyFunction:
Exit Function
Err_MyFunction:
gErrorHandler.DisplayError Err.Number, Err.Description, Erl, csModule, "basMyModuleName", "MyFunction"
Resume Exit_MyFunction
End Function
And here's the error dialog I get instead of having the error passed to the custom handler:
这是我得到的错误对话框,而不是将错误传递给自定义处理程序:
采纳答案by Lokerim
As Tim pointed out in his comment, the answer was that the VBA IDE was configured to break on all errors. Changing it to break on unhandled errors only gave me the behavior I desired.
正如 Tim 在他的评论中指出的那样,答案是 VBA IDE 被配置为中断所有错误。改变它以打破未处理的错误只会给我我想要的行为。