Excel VBA - 是否可以在出错时调用 sub?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26427715/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:54:07  来源:igfitidea点击:

Excel VBA - Is it possible to call sub on error?

excelvbaexcel-vbaerror-handling

提问by Goos van den Bekerom

I've been searching, but I can't really find something that answers this question.

我一直在寻找,但我真的找不到可以回答这个问题的东西。

I'm getting an error and I know whats causing it and it not really a problem. It just needs to run another sub before running the next line of code if this error occurs. So that made me wonder:

我收到一个错误,我知道是什么导致了它,这并不是真正的问题。如果发生此错误,它只需要在运行下一行代码之前运行另一个子程序。所以这让我想知道:

Is it possible to do something like this:

是否有可能做这样的事情:

On Error Call Sheet1.TestSub

Thanks in advance!

提前致谢!

回答by aebailey

You could try something like this:

你可以尝试这样的事情:

Sub test()
On Error GoTo 10
Set a = b
MsgBox ("still going")

Exit Sub
10:  test2
Resume Next

End Sub

Sub test2()
MsgBox ("Error")

End Sub

回答by Gary's Student

No.

不。

The best you can do is to branch to a separate section of code to handle the error and then branch back to the line immediately below the line raising the error. (if you know what that line is!)

您能做的最好的事情是分支到一个单独的代码部分来处理错误,然后分支回到紧接引发错误的行下方的行。(如果你知道那条线是什么!)