简体   繁体   English

仅处理特定的 VBA 错误

[英]Handling only specific VBA Errors

I have two errors that are possible when running my code.运行我的代码时,我有两个可能的错误。 The first is a common error in which my .Find method can't find anything, and I'd like it to resume next if this happens.第一个是一个常见错误,我的 .Find 方法找不到任何东西,如果发生这种情况,我希望它接下来继续。 It's a completely normal occurrence, and I need to leave it in for my manager to approve the code (legacy VBA code is still used and he's scared to change it.这是完全正常的情况,我需要将其留给我的经理批准代码(仍在使用旧版 VBA 代码,他害怕更改它。

I'd like to specify that if this error is seen then to do nothing, but if it's a specific other error to flag it and be handled by a more robust error handling.我想指出,如果看到此错误,则什么都不做,但如果是特定的其他错误,则标记它并由更强大的错误处理来处理。

The error I'd like to "ignore" (as in Resume Next or GoTo a specific place in the rest of the code without worrying about the error, I'm not worried about further down the code) is Runtime Error 91. Specifically in the code:我想“忽略”的错误(如 Resume Next 或 GoTo a specific place in the rest of the code 而不必担心错误,我不担心代码的进一步下层)是 Runtime Error 91。特别是在编码:

toFindCell1 = Cells.Find(nameVar).Row

where nameVar changes based on a for statement going down a list.其中 nameVar 根据列表中的 for 语句进行更改。 I plan to then check it against existing information and use that variable to determine whether or not it exists.我计划然后根据现有信息检查它并使用该变量来确定它是否存在。 If it doesn't, then it will add it.如果没有,那么它会添加它。

How can I specify the error I want to handle in VBA?如何指定要在 VBA 中处理的错误?

toFindCell1 = Cells.Find(nameVar).Row

Range.Find returns Nothing , the .Row member call isn't legal. Range.Find返回Nothing.Row成员调用不合法。 Don't do it!不要这样做!

If your code doesn't throw error 91 in the first place, then you don't need to handle error 91.如果您的代码一开始没有抛出错误 91,那么您就不需要处理错误 91。

Dim result As Range
Set result = Cells.Find(nameVar)
If Not result Is Nothing Then
    toFindCell1 = result.Row
    '....
Else
    'not found.
End If

The best practice is indeed to use the If Not result Is Nothing Then , as mentioned in the answer of Mathieu .最佳做法确实是使用If Not result Is Nothing Then ,如Mathieu的回答中所述。


However, by some specific cases it could be really a good idea to catch a specific error number and continue, by fixing it.但是,在某些特定情况下,通过修复它来捕获特定的错误编号并继续操作确实是一个好主意。 This is definitely not one of them, but is a good illustration , of how to "play" with Err.Number :这绝对不是其中之一,但很好地说明了如何“玩” Err.Number

Sub TestMe()

    On Error GoTo TestMe_Error

    Dim result As Range
    Set result = Cells.Find("Something")

    Debug.Print result.Row
    Debug.Print "Something here"
    Debug.Print 5 / 0
    Debug.Print "This line is unreachable."

TestMe_Error:

    Select Case Err.Number
    Case 91
        Debug.Print "ERROR 91!"
        Err.Clear
        Set result = Range("A100")
        Resume
    Case Else
        Debug.Print "Some other error!"
    End Select

End Sub

What is happening in the code above?上面的代码发生了什么? On line Debug.Print result.Row it throws error 91 , which is caught by the error handler and then cleared with Err.Clear .Debug.Print result.Row它会抛出error 91 ,错误处理程序会捕获该错误,然后使用Err.Clear清除该错误。 As this is expected, Set result = Range("A100") is used in the error handler and the code continues from the line, which threw the error, but this time, result is valid.正如预期的那样,在错误处理程序中使用了Set result = Range("A100")并且代码从抛出错误的行继续,但这次result是有效的。 Once, it reaches the new error 5/0 , then it throws error, different than 91 and exits.一次,它到达新错误5/0 ,然后抛出错误,不同于91并退出。

This is the output:这是输出:

ERROR 91!
 100 
Something here
Some other error!

Keep in mind that using conditional error handling could be considered spaghetti code by plenty of devs.请记住,许多开发人员可能会将使用条件错误处理视为意大利面条式代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM