处理VBA时出现错误? [英] Error handling VBA : when no errors?

查看:133
本文介绍了处理VBA时出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VBA捕获一些错误,我发现本教程我想使用 GoTo 方法捕获错误,如下例所示:

I'm trying to catch some errors with VBA, i found this tutorial and i want to catch the errors using the GoTo method like in the following example :

Sub mySub
 On Error GoTo myHandler:
    Workbooks.Open("myWorkbook")
'
' Some Code
'
myHandler:
MsgBox "EROOR !"

End Sub

问题是即使没有错误,始终执行 myHandler 部分!
我发现这个讨论但建议的答案不能解决问题

我试图添加一个 Exit Sub 语句,如下所述:

The problem is that even if there is no error, the myHandler section is always executed ! I found this discussion but the proposed answer doesn't solve the problem
I tried to add an Exit Sub statement as explained :

Sub mySub
 On Error GoTo myHandler:
    Workbooks.Open("myWorkbook")
    Exit Sub

'
' Some Code
'
myHandler:
  MsgBox "EROOR !"

End Sub

在这种情况下,即使没有错误也会退出该方法。
我也试过:

In that case it exits the method even without error. I also tried :

 Sub mySub
 On Error GoTo myHandler:
    Workbooks.Open("myWorkbook")
'
' Some Code
'
myHandler:
  MsgBox "EROOR !"
  Exit Sub
End Sub

但是仍然是同样的问题,myHandler是执行甚至没有错误。

But still the same problem, the myHandler is executed even without errors.

推荐答案

只需将退出子进入。

Sub mySub
 On Error GoTo myHandler:
    Workbooks.Open("myWorkbook")
'
' Some Code
'
Exit sub

myHandler:
MsgBox "EROOR !"

err.clear
End Sub

这篇关于处理VBA时出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆