我写了一些例子来强调我所理解的问题。
1.示例:可以正常工作。通过Goto
语句处理错误,并将错误处理返回到正常行为
Sub ErrorHandlingWithoutLoop()
Debug.Print "Before Error"
On Error GoTo errorHandler
Error (1)
' If no Error occured, the ErrorHandling procedure is skipped
GoTo skip
errorHandler:
Debug.Print "Handle Error"
On Error GoTo 0
skip:
Debug.Print "After Error"
'Raise Error to see if error Handling is resumed to normal behavior
Error (14)
End Sub
VBA返回:Before Error
Handle Error
After Error
并且错误处理将恢复正常,可以通过抛出的错误看到(14)示例2:我希望这个子程序运行得很好,并且在最后一行中抛出错误,并显示错误(14)。
但是,在第二个循环中,n = 2,错误处理无法按预期进行。为什么?
Sub ErrorHandlingWithLoop()
Debug.Print "Before Error"
For n = 1 To 10
On Error GoTo errorHandler
Error (1)
' If no Error occured, the ErrorHandling procedure is skipped
GoTo skip
errorHandler:
Debug.Print "Handle Error", n
On Error GoTo 0
skip:
Debug.Print "After Error", n
Next n
'Raise Error to see if error Handling is resumed to normal behavior
' -> This wont be reached though
Error (14)
End Sub
VBA返回:Before Error
Handle Error 1
After Error 1
代码的执行在第二个循环中以n = 2停止,抛出错误为1。不抛出最后的错误14。示例3:我知道我可以通过添加“恢复跳过”语句来解决此问题。 但是我不明白为什么这样做是必要的?
Sub ErrorHandlingWithLoopFixed()
Debug.Print "Before Error"
For n = 1 To 10
On Error GoTo errorHandler
Error (1)
' If no Error occured, the ErrorHandling procedure is skipped
GoTo skip
errorHandler:
Debug.Print "Handle Error", n
On Error GoTo 0
Resume skip
skip:
Debug.Print "After Error", n
Next n
'Raise Error to confirm that error Handling is resumed to normal behavior
Error (14)
End Sub
VBA返回:Before Error
Handle Error 1
After Error 1
Handle Error 2
After Error 2
Handle Error 3
After Error 3
Handle Error 4
After Error 4
Handle Error 5
After Error 5
Handle Error 6
After Error 6
Handle Error 7
After Error 7
Handle Error 8
After Error 8
Handle Error 9
After Error 9
Handle Error 10
After Error 10
并且错误处理将恢复正常,可以通过抛出的错误看到(14)那么为什么必须跳过“继续”?
感谢您的帮助=)
编辑:
!Bonus示例!3:有趣的是,如果我添加
On Error Goto -1
,则for循环会无限循环。这是为什么?我不能为此押韵。Sub ErrorHandlingWithLoop()
Debug.Print "Before Error"
For n = 1 To 10
On Error GoTo errorHandler
Error (1)
' If no Error occured, the ErrorHandling procedure is skipped
GoTo skip
errorHandler:
Debug.Print "Handle Error", n
On Error GoTo -1
skip:
Debug.Print "After Error", n
Next n
'Raise Error to see if error Handling is resumed to normal behavior
' -> This wont be reached though
Error (14)
End Sub
VBA返回:Before Error
Handle Error 1
After Error 1
Handle Error 2
After Error 2
Handle Error 3
After Error 3
Handle Error 4
After Error 4
Handle Error 5
After Error 5
Handle Error 6
After Error 6
Handle Error 7
After Error 7
Handle Error 8
...
...
...
这会一直持续下去,直到Excel崩溃为止。对此也有帮助吗?
最佳答案
就个人而言,我不会遵循您提供的示例来处理错误。取而代之的是,我将封装可能会导致其自身错误的操作,该操作根据操作是否成功而返回true或false。使用ByRef参数将操作返回的值传递给函数。
Sub ErrorHandlingWithLoopFixed()
Debug.Print "Before Error"
Dim myResult as Variant
For n = 1 To 10
If Not TryAction( myResult) then
Debug.Print "An error occurred"
Stop ' or exit function or whatever
end if
' Continue processing myResult
Next n
'Raise Error to confirm that error Handling is resumed to normal behavior
Error (14)
End Sub
Public Function TryAction( ByRef opResult) as Boolean
On Error Resume Next
opResult = ' the action
TryAction = err.number = 0
On Error Goto 0
Exit Function
从上面可以看到,代码更容易遵循,跳入和跳出For循环的范围也没有问题。
关于vba - 了解for循环VBA中的错误处理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65599799/