gpt4 book ai didi

excel - VBA On Error 语句执行时没有错误

转载 作者:行者123 更新时间:2023-12-03 02:04:39 25 4
gpt4 key购买 nike

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

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

最佳答案

您应该在实际错误处理程序之前添加一个退出,并在显示对话框后恢复默认错误处理。 (第一个错误 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

关于excel - VBA On Error 语句执行时没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24037844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com