简体   繁体   English

Excel VBA-是否可以在发生错误时调用sub?

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

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! 提前致谢!

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

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!) (如果您知道那条线是什么!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM