vba:将变量传递到错误句柄

vba:将变量传递到错误句柄,vba,Vba,我有一个声明: on error go to label 但是,我希望将导致错误的变量传递到标签中 这可能吗?首先,我想你的意思是: on error goto label 不,不能使用goto命令传递变量。但是,您可以查看Err.Description以了解详细信息,如果您提出自己的错误,可以执行以下操作: ' Raise a custom error. Err.Raise Number:=vbObjectError + 1000, _ Source:="Tes

我有一个声明:

on error go to label
但是,我希望将导致错误的变量传递到标签中


这可能吗?

首先,我想你的意思是:

on error goto label
不,不能使用goto命令传递变量。但是,您可以查看Err.Description以了解详细信息,如果您提出自己的错误,可以执行以下操作:

  ' Raise a custom error.
    Err.Raise Number:=vbObjectError + 1000, _
        Source:="TestRaiseCustomError", _
        Description:="My custom error description."
因此,如果您提出自己的错误,可以将Source设置为导致问题的字段


有关更多信息,请参阅上的“使用错误对象的提升方法来提升自定义错误”部分。

首先,我认为您的意思是:

on error goto label
不,不能使用goto命令传递变量。但是,您可以查看Err.Description以了解详细信息,如果您提出自己的错误,可以执行以下操作:

  ' Raise a custom error.
    Err.Raise Number:=vbObjectError + 1000, _
        Source:="TestRaiseCustomError", _
        Description:="My custom error description."
因此,如果您提出自己的错误,可以将Source设置为导致问题的字段


有关更多信息,请参阅中的“使用错误对象的提升方法提升自定义错误”部分。

您可以使用Err获取错误号和说明

 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

您可以使用Err获取错误号和描述

 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

我想不出一个聪明的办法来做这件事。我通常有一个错误处理类/函数,这样我可以在错误转到时使用它将错误传递到底部块,然后调用错误处理函数。这样做的好处是有一个集中的错误处理程序很好,但是你也可以自定义它,所以在我的例子中,我传递崩溃过程的名称。这并不漂亮,但是如果你真的想传递一个变量集合(取决于你有多少个变量),或者根据你必须添加的行号设置一些东西来识别变量,你可以

on error goto err

'Code

err:

ErrorHandeler err, "String with Procedure name"

我想不出一个聪明的办法来做这件事。我通常有一个错误处理类/函数,这样我可以在错误转到时使用它将错误传递到底部块,然后调用错误处理函数。这样做的好处是有一个集中的错误处理程序很好,但是你也可以自定义它,所以在我的例子中,我传递崩溃过程的名称。这并不漂亮,但是如果你真的想传递一个变量集合(取决于你有多少个变量),或者根据你必须添加的行号设置一些东西来识别变量,你可以

on error goto err

'Code

err:

ErrorHandeler err, "String with Procedure name"

声明全局变量并在代码和错误代码中使用它们

Public global_variable1 As Integer
Public global_variable2 As String

Private Sub Btn1234_Click()
....
end sub

Err_abcd:                           
....
End Sub

声明全局变量并在代码和错误代码中使用它们

Public global_variable1 As Integer
Public global_variable2 As String

Private Sub Btn1234_Click()
....
end sub

Err_abcd:                           
....
End Sub