2010-11-19 98 views

回答

3

首先,我認爲你的意思是:

on error goto label 

不,你不能通過使用GOTO命令變量。但是,您可以檢查細節Err.Description,如果你提出你自己的錯誤,你可以這樣做:

' Raise a custom error. 
    Err.Raise Number:=vbObjectError + 1000, _ 
     Source:="TestRaiseCustomError", _ 
     Description:="My custom error description." 

所以,如果你提出自己的錯誤,你可以設置源到戰場。造成了這個問題。

請參閱使用錯誤對象的提升方法來提高自定義錯誤部分this link以獲取更多信息。

4

您可以使用錯誤來得到錯誤號和說明

Sub|Function SomeName() 
    On Error GoTo Err_SomeName   ' Initialize error handling. 
    ' Code to do something here. 
Exit_SomeName:       ' Label to resume after error. 
    Exit Sub|Function     ' Exit before error handler. 
Err_SomeName:       ' Label to jump to on error. 
    MsgBox Err.Number & Err.Description ' Place error handling here. 
    Resume Exit_SomeName    ' Pick up again and quit. 
End Sub|Function 
0

我想不出一個聰明的辦法做到這一點。我通常有一個錯誤handeling類/函數,我可以使用「錯誤goto」將錯誤傳遞給底部塊,然後調用錯誤handeling函數。這樣做的好處是有一個集中的錯誤處理程序是很好的,但也可以自定義它,所以在我的情況下,我傳遞了程序崩潰的名稱。這並不漂亮,但如果你真的想要傳遞一組變量(取決於你有多少變量),或者設置一些東西來根據行號識別變量(你必須添加manuly ...) )

on error goto err 

'Code 

err: 

ErrorHandeler err, "String with Procedure name" 
0

聲明全局變量並在代碼和錯誤代碼中使用它們。

Public global_variable1 As Integer 
Public global_variable2 As String 

Private Sub Btn1234_Click() 
.... 
end sub 

Err_abcd:       
.... 
End Sub