正确处理VBA中的错误(Excel) [英] Properly Handling Errors in VBA (Excel)

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

问题描述

我一直在VBA工作一段时间,但我仍然不太了解错误处理。



一篇很好的文章是
CPearson.com



然而,我仍然想知道如果我以前做错误处理的方式是/是完全错误的:
块1

  On Error Goto ErrCatcher 
如果UBound(.sortedDates)> 0然后

//代码

Else
ErrCatcher:
//代码

结束如果

if子句,因为如果它是true,它将被执行,如果它失败,Goto将进入Else-一部分,由于Array的Ubound不应该是零或更少,没有错误,所以这个方法迄今为止非常好。



如果我理解它是正确的,应该是像这样:
块2

 错误转到ErrCatcher 
如果Ubound (.sortedDates)> 0然后

//代码
结束如果

转到这里X

ErrCatcher:
//代码
简历/简历下一页/简历hereX

hereX:

甚至这样:
阻止3

  On Error Goto ErrCatcher 
如果Ubound(。 sortedDates)> 0然后

//代码
结束如果

ErrCatcher:
如果Err.Number<> 0然后
//代码
结束如果

我最常见的方式是那个,那个错误捕手是在一个sub的结尾,Sub实际上结束之前有一个退出子,但是如果Sub相当大,如果你跳转反之亦然,这不是有点混乱阅读代码?



阻止4


以下代码的来源:
CPearson.com




 错误转到ErrHandler:
N = 1/0导致错误
'
'更多代码
'
退出子

ErrHandler:

'错误处理代码'

Resume Next

End Sub

是否应该像块3? / p>

感谢您阅读我的问题
问候语
skofgar

解决方案

我绝对不会使用Block1。在与ERROR无关的IF语句中看起来似乎没有错误。



阻止2,3和我猜是一个主题的变化。我更喜欢使用Block 3& 4超过2只因为不喜欢GOTO声明;我通常使用Block4方法。这是我用来检查Microsoft ActiveX数据对象2.8库是否被添加的代码的一个例子,如果没有添加,或者如果不可用,请使用较早的版本。

  Option Explicit 
Public booRefAdded As Boolean'一次查询引用

Public Sub Add_References()
Dim lngDLLmsadoFIND As Long

如果不是booRefAdded然后
lngDLLmsadoFIND = 28'加载msado28.tlb,如果找不到降压版本,直到找到

错误GoTo RefErr:
'添加Microsoft ActiveX数据对象2.8
Application.VBE.ActiveVBProject.references.AddFromFile _
Environ(CommonProgramFiles)+\System\ado\msado& lngDLLmsadoFIND& .tlb

错误GoTo 0

退出子

RefErr:
选择案例Err.Number
案例0
'没有错误
案例1004
'启用信任中心设置
MsgBox(某些VBA参考不可用,允许访问遵循这些步骤& Chr(10) & _
转到Excel选项/信任中心/信任中心安全/宏设置& Chr(10)& _
1.勾选 - 禁用所有具有通知的宏& Chr(10)& _
2. Tick - 信任访问VBA项目对象模型
End
案例32813
'Err.Number 32813表示参考已经添加
案例48
'引用不存在
如果lngDLLmsadoFIND = 0然后
MsgBox(找不到必需的重新ference)
End
Else
对于lngDLLmsadoFIND = lngDLLmsadoFIND - 1至0步骤-1
简历
下一页lngDLLmsadoFIND
如果

Case Else
MsgBox Err.Number& vbCrLf& Err.Description,vbCritical,Error!
结束
结束选择

错误GoTo 0
结束如果
booRefAdded = TRUE
End Sub


I've been working with VBA for quite a while now, but I'm still not so sure about Error Handling.

A good article is the one of CPearson.com

However I'm still wondering if the way I used to do ErrorHandling was/is completely wrong: Block 1

On Error Goto ErrCatcher
   If UBound(.sortedDates) > 0 Then

       // Code

   Else
ErrCatcher:
       // Code

   End If

The if clause, because if it is true, it will be executed and if it fails the Goto will go into the Else-part, since the Ubound of an Array should never be zero or less, without an Error, this method worked quite well so far.

If I understood it right it should be like this: Block 2

On Error Goto ErrCatcher
    If Ubound(.sortedDates) > 0 Then

       // Code
    End If

    Goto hereX

ErrCatcher:
       //Code
    Resume / Resume Next / Resume hereX

hereX:

Or even like this: Block 3

On Error Goto ErrCatcher
    If Ubound(.sortedDates) > 0 Then

       // Code
    End If

ErrCatcher:
    If Err.Number <> 0 then
       //Code
    End If

The most common way I see is that one, that the Error "Catcher" is at the end of a sub and the Sub actually ends before with a "Exit Sub", but however isn't it a little confusing if the Sub is quite big if you jump vice versa to read through the code?

Block 4

Source of the following Code: CPearson.com

  On Error Goto ErrHandler:
   N = 1 / 0    ' cause an error
   '
   ' more code
   '
  Exit Sub

  ErrHandler:

   ' error handling code'

   Resume Next

End Sub 

Should it be like in Block 3 ?

Thank you for reading my question Greetings skofgar

解决方案

I definitely wouldn't use Block1. It doesn't seem right having the Error block in an IF statement unrelated to Errors.

Blocks 2,3 & 4 I guess are variations of a theme. I prefer the use of Blocks 3 & 4 over 2 only because of a dislike of the GOTO statement; I generally use the Block4 method. This is one example of code I use to check if the Microsoft ActiveX Data Objects 2.8 Library is added and if not add or use an earlier version if 2.8 is not available.

Option Explicit
Public booRefAdded As Boolean 'one time check for references

Public Sub Add_References()
Dim lngDLLmsadoFIND As Long

If Not booRefAdded Then
    lngDLLmsadoFIND = 28 ' load msado28.tlb, if cannot find step down versions until found

        On Error GoTo RefErr:
            'Add Microsoft ActiveX Data Objects 2.8
            Application.VBE.ActiveVBProject.references.AddFromFile _
            Environ("CommonProgramFiles") + "\System\ado\msado" & lngDLLmsadoFIND & ".tlb"

        On Error GoTo 0

    Exit Sub

RefErr:
        Select Case Err.Number
            Case 0
                'no error
            Case 1004
                 'Enable Trust Centre Settings
                 MsgBox ("Certain VBA References are not available, to allow access follow these steps" & Chr(10) & _
                 "Goto Excel Options/Trust Centre/Trust Centre Security/Macro Settings" & Chr(10) & _
                 "1. Tick - 'Disable all macros with notification'" & Chr(10) & _
                 "2. Tick - 'Trust access to the VBA project objects model'")
                 End
            Case 32813
                 'Err.Number 32813 means reference already added
            Case 48
                 'Reference doesn't exist
                 If lngDLLmsadoFIND = 0 Then
                    MsgBox ("Cannot Find Required Reference")
                    End
                Else
                    For lngDLLmsadoFIND = lngDLLmsadoFIND - 1 To 0 Step -1
                           Resume
                    Next lngDLLmsadoFIND
                End If

            Case Else
                 MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
                End
        End Select

        On Error GoTo 0
End If
booRefAdded = TRUE
End Sub

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

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