4

有没有办法说是否出现错误 1004,显示消息“消息”,如果错误 9,显示消息“消息 2”,而不是最终用户的通用非描述符极客讲话消息?

4

1 回答 1

7

您正在尝试做的事情称为错误处理。

请参阅此示例。您可以使用以下方法捕获错误号Err.Number

Sub Sample()
    On Error GoTo Whoa

    '~~> Rest of the code

    Exit Sub
Whoa:
    Select Case Err.Number
        Case 9
            MsgBox "Message1"
        Case 1004
            MsgBox "Message2"
    End Select
End Sub

跟进

Sub Sample1()
    On Error GoTo Whoa

    '~~> Rest of the code

    Exit Sub
Whoa:
    MsgBox GetErrMsg(Err.Number)
End Sub

Sub Sample2()
    On Error GoTo Whoa

    '~~> Rest of the code

    Exit Sub
Whoa:
    MsgBox GetErrMsg(Err.Number)
End Sub

Function GetErrMsg(ErNo As Long) As String
    Select Case ErNo
        Case 9
            GetErrMsg = "Message1"
        Case 1004
            GetErrMsg = "Message2"
        Case Else
            GetErrMsg = "Message3"
    End Select
End Function
于 2012-07-16T14:25:29.963 回答