VBA:显示标准运行时错误处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4558300/
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
VBA: Displaying the standard run-time error handler
提问by strcompnice
I have a question about the correct way to handle errors in VBA in Excel. If a specific error, say xxxxxxx, occurs, then a MsgBox should be displayed. If another error occurs the standard run-time error handler should pop up. How can this be accomplished? Here is the example code:
我有一个关于在 Excel 中处理 VBA 错误的正确方法的问题。如果发生特定错误,例如 xxxxxxx,则应显示 MsgBox。如果发生另一个错误,标准运行时错误处理程序应该弹出。如何做到这一点?这是示例代码:
On Error Resume Next
'Line of code that causes an error here.
If Err.Number = xxxxxxx Then
MsgBox "Specific error message."
ElseIf Err.Number = 0 Then
Do nothing
Else 'Some error other than xxxxxxx.
'This is the problem. Here I would like to display standard run-time error
'handler without causing the error again.
End If
On Error GoTo 0
采纳答案by JeffK
You can get a message box that looks very much like the standard error message by putting this into your "Else" block:
通过将其放入“Else”块中,您可以获得一个与标准错误消息非常相似的消息框:
MsgBox "Run-time error '" & Err.Number & "':" & _
vbNewLine & vbNewLine & _
Error(Err.Number), vbExclamation + vbOKOnly, _
"YourProjectNameHere"
But this is just a facsimile. It's not the actual error message dialog that VB6 puts up; it's just formatted to look like it. Error handling is still disabled by the "On Error Resume Next" statement at this point.
但这只是一个传真。这不是 VB6 提供的实际错误消息对话框;它只是格式化为看起来像。此时,“On Error Resume Next”语句仍禁用错误处理。
But if you really, reallywant to invoke the standard error handling code, you can put this in the "Else" block:
但是如果你真的,真的想调用标准错误处理代码,你可以把它放在“Else”块中:
Dim SaveError As Long
SaveError = Err.Number
On Error Goto 0
Error (SaveError)
This code saves the error number, re-enables error handling, and then re-raises the error. You invoke the VB runtime's true error handling machinery this way. But beware: if this error isn't caught with an active error handler somewhere higher in the call chain, it will terminate your programafter the user clicks on the "OK" button.
此代码保存错误编号,重新启用错误处理,然后重新引发错误。您可以通过这种方式调用 VB 运行时真正的错误处理机制。但请注意:如果此错误未被调用链中某个位置的活动错误处理程序捕获,它将在用户单击“确定”按钮后终止您的程序。
Note that you'll also lose the ability to get the actual line number where the error occurs using" Erl" in that error handler because you are re-generating the runtime error with the "Error (SaveError)" statement. But that probably won't matter because most VB code doesn't actually use any line numbers, so Erl just returns 0 anyway.
请注意,您还将无法在该错误处理程序中使用“Erl”获取发生错误的实际行号,因为您正在使用“Error (SaveError)”语句重新生成运行时错误。但这可能无关紧要,因为大多数 VB 代码实际上并不使用任何行号,因此 Erl 无论如何都只返回 0。
回答by strcompnice
So to follow up on JeffK's new suggestion the code below seems to work fine with VBA, and what's more I can't see the danger in using it. To terminate Excel is critical as that could lose a lot of work, but since the code always checks that the error is there how could that happen?
因此,为了跟进 JeffK 的新建议,下面的代码似乎可以与 VBA 一起正常工作,而且我看不到使用它的危险。终止 Excel 至关重要,因为这可能会丢失大量工作,但由于代码总是检查错误是否存在,怎么会发生这种情况?
Thank you JeffK for this intriguing idea.
谢谢 JeffK 这个有趣的想法。
Dim savedNumber As Long
On Error Resume Next
'Line of code that causes an error.
If Err.Number = XXXXXXX Then
'Specific error message.
ElseIf Err.Number <> 0 Then
savedNumber = Err.Number
On Error GoTo 0
Error savedNumber
End If
Err.Clear
On Error GoTo 0
回答by N0Alias
Replace On Error Resume Next with
替换 On Error Resume Next 为
On Error Goto SomePlaceInCodeToHandleErrors
SomePlaceInCodeToHandleErrors:
If Err.Number = XXXX Then
MSGBOX "Message"
End If
Check out this Stack Overflow threadfor some more information and example code.
查看此Stack Overflow 线程以获取更多信息和示例代码。
回答by Patrick Honorez
In your VBA options, select "Break on unhandled errors".
To enable handling use on error goto SomeLabel
or on error resume next
.
To stop error handling use on error goto 0
.
在 VBA 选项中,选择“中断未处理的错误”。
要启用处理,请使用on error goto SomeLabel
或on error resume next
。
要停止错误处理,请使用on error goto 0
.
Your question is contradictory in this context. If you enable error handling, well, you disable the standard error handling.
在这种情况下,您的问题是矛盾的。如果您启用错误处理,那么您将禁用标准错误处理。
As DaMartyr suggests, you can still use something like msgbox err.description
.
正如 DaMartyr 建议的那样,您仍然可以使用类似msgbox err.description
.