VBA On Error语句执行无错误



我使用下面的简单代码来处理错误。在没有错误的情况下,为什么会出现提示框?如何使提示框只在出现错误时才出现?

Sub Test()
On Error GoTo ErrHandler
On Error GoTo 0

'rest of the code is here
ErrHandler:
MsgBox "Please make sure the file exists in the current folder."
Exit Sub
End Sub

应该在实际的错误处理程序之前添加退出,并在显示对话框之后恢复默认的错误处理程序。(第一个on错误goto 0可能放错地方了)。

Sub Test()
On Error GoTo ErrHandler

'rest of the code is here
'Exit before error handlers
Exit Sub
ErrHandler:
  MsgBox "Please make sure the file exists in the current folder."
  ' Reset error handler
  On Error GoTo 0
  Exit Sub
End Sub

最新更新