簡體   English   中英

在 VBA 中處理錯誤時如何管理無錯誤情況?

[英]How to manage the no error case when handling errors in VBA?

我需要使用GoTo語句捕獲一些 VBA 錯誤:

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

End Sub

問題是當沒有錯誤時,會執行errorHandler部分。
我找到了這個討論,但答案並沒有解決我的問題。
我嘗試按照說明添加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

只需將 Exit 子放入即可。

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

myHandler:
MsgBox "EROOR !"

err.clear
End Sub

這是我更喜歡的模式:

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. 習慣性地在錯誤處理塊之前插入 exit 塊可以減少我遇到 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
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

在錯誤處理程序部分使用以下代碼:

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

我和你遇到了完全相同的問題,上面的解決方案不起作用。 他們顯然甚至沒有看到您在原始帖子的 2 個不同地方已經寫了 Exit Sub。 似乎沒有任何在線網站理解有時不會出現錯誤(如果總是會出現錯誤,您為什么要那樣編碼?),而當沒有錯誤時,您顯然不會想退出子。 您也不希望 myHandler 在沒有錯誤時運行。 呸! 這是我想出的似乎有效的解決方案。

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

我在 ErrorHandler 中使用 If 語句,如果沒有錯誤,它將停止執行。 這是通過使用 Err.Number (Err (object) number (eg Run-time error 9: Subscript out of range)) 來實現的

If Err.Number >= 1 Then
MsgBox ("Message")
End
Else: other code
End If
Exit Sub

這就是我所做的。 奇跡般有效

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
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM