2012-04-13 78 views
4

我試圖趕上與VBA一些錯誤,我發現this tutorial,我想用GoTo方法就像在下面的例子中捕捉錯誤:錯誤處理VBA:什麼時候沒有錯誤?

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

End Sub 

的問題是,即使沒有錯誤,myHandler部分始終執行! 我發現this discussion但作爲解釋所提出的答案不能解決問題
我嘗試添加一個Exit Sub聲明:

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

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

End Sub 

在它退出方法,即使沒有錯誤的情況。 我也試過:

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

但還是同樣的問題,將myHandler即使沒有錯誤執行。

回答

9

只要把退出子在

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

myHandler: 
MsgBox "EROOR !" 

err.clear 
End Sub 
+1

你可以添加'On Error Goto 0'這行嗎?所以OP知道這是一個很好的做法重置錯誤重定向... Upvoted :) – Marco 2012-04-13 13:02:33

+0

@Marco你的意思是err.Clear?這是VBA。 – Fionnuala 2012-04-13 13:05:14

+0

@Marco我應該重置它後每個捕捉集團? 'On Error GoTo myHandler: 工作簿。打開(「我的工作簿」) On Error GoTo 0' – M3HD1 2012-04-13 13:07:03

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 
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. 習慣性地將出口塊之前的錯誤處理塊減少,我就會有意外掉落到錯誤處理程序的任擇議定書的問題的機會。
  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 
    
+0

是的,我也喜歡。它似乎也是自動插入錯誤處理塊的應用程序的標準。我甚至有一些代碼可以刪除,並以這種格式創建輪廓錯誤處理。 – Fionnuala 2012-04-15 07:36:16

0

使用下面的代碼中的錯誤處理程序部分:

如果Err.Number的> 0 '錯誤處理到這裏 其他 ' 如果

0

我 結束與您具有完全相同的問題,並且上述解決方案不起作用。他們顯然甚至沒有看到你在原帖中的2個不同的地方寫了Exit 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 
+0

無論是否存在錯誤,接受的答案都應該有效,這實際上是處理VBA中錯誤的正確方法。另一方面,使用'GoTo'來移動代碼被認爲是非常糟糕的做法,特別是如果您將使用多個'GoTo'語句。 – 2017-01-31 17:25:18

+0

OP把'Exit Sub'放在兩個錯誤的地方。你只需要把它放在正確的地方(即就在你的錯誤處理代碼之前)。 – 2017-01-31 17:25:34