我发现自己在过程结束时和错误处理程序中都编写了一些命令。最重要的是,我发现自己在所有函数中都写了这些行:
Application.Cursor = xlDefault
Application.ScreenUpdating = True
ErrHandler:
MsgBox ("An unforseen problem has occured. Please contact support.")
Application.Cursor = xlDefault
Application.ScreenUpdating = True
End Sub
我觉得我重复自己的次数超出了我的预期。在过程结束和错误处理程序中编写这些“标准”行是否有更好的做法?
最佳答案
您可以简单地让您的错误处理程序在代码的“清理”部分恢复,当没有错误发生时也会执行该部分:
Clean_up:
Application.Cursor = xlDefault
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox "An unforeseen problem has occurred. Please contact support."
Resume Clean_up
End Sub
如果您的例程中总是有该代码,您可以将其移至您从
Clean_up
调用的单独例程中。部分。
关于vba - 在过程结束和错误处理程序中编写结束语句的好习惯是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26630048/