如何在VBA中处理错误时管理无错误案例?

时间:2012-04-13 12:58:50

标签: excel vba error-handling

我需要使用GoTo语句捕获一些VBA错误,如下所示:

Sub mySub
 On Error GoTo errorHandler:
    Workbooks.Open("myWorkbook")
'
' Some Code
'
errorHandler:
MsgBox "ERROR"

End Sub

问题在于,当没有错误时,errorHandler部分被执行 我找到了this discussion,但答案并没有解决我的问题 我尝试添加Exit Sub语句,如下所述:

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

'
' Some Code
'
errorHandler:
  MsgBox "ERROR"

End Sub

在这种情况下,它会在没有错误时退出方法。 我也尝试过:

 Sub mySub
 On Error GoTo errorHandler:
    Workbooks.Open("myWorkbook")
'
' Some Code
'
errorHandler:
  MsgBox "ERROR"
  Exit Sub
End Sub

但仍然存在同样的问题:即使没有发生错误,也会执行errorHandler

8 个答案:

答案 0 :(得分:12)

只需将Exit sub放入。

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

myHandler:
MsgBox "EROOR !"

err.clear
End Sub

答案 1 :(得分:4)

这是我喜欢的模式:

Sub SomeSub()
    On Error GoTo ErrorLabel

    'Code goes here

ExitLabel:
   'Clean-up code, if any, goes here 
   Exit Sub

ErrorLabel:
    'Error-handling code goes here
    Resume ExitLabel
End Sub

请注意Resume清除错误。我喜欢这种模式有几个原因:

  1. 在错误处理块之前习惯性地插入出口块可以减少我出现意外掉入错误处理程序的OP问题的可能性。
  2. 我使用GoTo ExitLabel任何提前退出Sub或Function。这样,我不太可能意外地跳过清理代码。例如:

    Sub SomeOtherSub()
        Dim x As ResourceThatNeedsToBeClosed
        Dim i As Long
        On Error GoTo ErrorLabel
        Set x = GetX
        For i = 1 To 100
            If x.SomeFunction(i) Then
                GoTo ExitLabel
            End If
        Next
    ExitLabel:
        x.Close
    ErrorLabel:
        'Error-handling code goes here
        Resume ExitLabel
    End Sub
    

答案 2 :(得分:1)

Public Sub MySub
    On Error Goto Skip

    ' Some Codes

Skip:
    If err.Number > 0 Then

        ' Run whatever codes if error occurs

        err.Clear
    End If
    On Error Goto 0
End Su

答案 3 :(得分:0)

在错误处理程序部分使用以下代码:

if err.number>0 the
    ' error handling goes here
else
    ' some code
end if

答案 4 :(得分:0)

我遇到与您完全相同的问题,上述解决方案无效。他们显然甚至没有看到你在原帖中的2个不同位置写过Exit Sub。没有在线网站似乎明白,有时候不会出现错误(如果总是会出现错误,为什么会这样编码呢?),并且当没有错误时,你显然不想退出Sub。当没有错误时,您也不希望myHandler运行。 DUH!这是我提出的解决方案似乎有效。

On Error GoTo ErrorHandler
'This is the thing I am trying to do...
Workbooks("SpreadSolver.xlsb").Activate
'If it works, great. 
'Don't touch the error stuff and move on. 
'I.e. go to the part that I know works (the rest of the macro)
GoTo ThisPartWorks

'If the thing I am trying to do doesn't work...
ErrorHandler:
MsgBox "Error: Please open Spread Solver and then run the macro."
'Only want to Exit Sub if there is an error.. duh.
Exit Sub

ThisPartWorks:
'the rest of your macro can go here...
'...
End Sub

答案 5 :(得分:0)

我在ErrorHandler中使用If语句,如果没有错误,它将停止执行。这是通过使用Err.Number(Err(对象)编号(例如,运行时错误9:下标超出范围))实现的

    <link rel="stylesheet" href="style.сss">

答案 6 :(得分:0)

这就是我所做的。像魅力一样工作

Sub someProgram ()
    On Error goto Handler:
        x = 7/0

    'Some code you wanna execute  with or without error
Exit Sub

Handler:
     'Write your handler here

Resume next 'After handling error, this line will help you resume the next line

End sub

答案 7 :(得分:0)

sub XYZ ()
on error goto label

"Write your macro"

Label:
      If Err.Description <> "" Then
       "your code if error occurs for eg:"
       msgbox "Error Occured!"
       Exit Sub
       Else
       "Your code when no error occurs for eg"
        msgbox " Done."
       End If
Exit Sub