如何阻止excel运行所有错误

时间:2017-06-28 10:04:08

标签: excel excel-vba error-handling vba

我有以下代码来处理我的错误:

Sub some_sub()

    On Error GoTo error1
    Some code here

    On Error GoTo error2
    Some more code here

    On Error GoTo error3
    final piece of code here



Exit Sub

error1
    MsgBox "Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write"

error2
    MsgBox "Please make sure that the Claims followup file is open." & Chr(10) & Chr(10) & "If the file is open make sure that you the Solicitation Number is written correctly."

error3
    MsgBox "Your Claim followup file is in -read only- mode. Your changes may not be saved"

End Sub
Sub some_sub()

    On Error GoTo error1
    Some code here

    On Error GoTo error2
    Some more code here

    On Error GoTo error3
    final piece of code here



Exit Sub

error1
    MsgBox "Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write"

error2
    MsgBox "Please make sure that the Claims followup file is open." & Chr(10) & Chr(10) & "If the file is open make sure that you the Solicitation Number is written correctly."

error3
    MsgBox "Your Claim followup file is in -read only- mode. Your changes may not be saved"

End Sub

然而,当" error1"检测到它发生了火灾" error2"和"错误3"同样,但我只想要它解雇" error1"。如果" error2"火灾"错误3"火也是。当error3被触发时,它是唯一的一个,所以它从它发现的错误中自上而下运行。

我的问题是:如何更改此代码,以便只触发错误之一?

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

尝试在消息框后面添加一行Exit Sub error1 MsgBox "Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write" Exit Sub error2 MsgBox "Please make sure that the Claims followup file is open." & Chr(10) & Chr(10) & "If the file is open make sure that you the Solicitation Number is written correctly." Exit Sub error3 MsgBox "Your Claim followup file is in -read only- mode. Your changes may not be saved" Exit Sub End Sub 。在error1的消息框之后继续执行。

所以看起来应该是这样的:

> dt <- data.table(ch = c("573427/02", "01/17", "030845/84", "15/03", "01", "02", "03", "56/03"), seq = c("23", "32456", "13", "657489", "879605", "564734", "657432", "657431"))
> dt
          ch    seq
1: 573427/02     23
2:     01/17  32456
3: 30845/84     13
4:     15/03 657489
5:        01 879605
6:        02 564734
7:        03 657432
8:     56/03 657431

答案 1 :(得分:0)

如果您尝试区分错误,可以使用Err.Number进行区分并捕获特定错误并相应地显示消息,所以在您的情况下应该是:

    `Sub some_sub()

        On Error GoTo errorHandler
        Some code here   


    Exit Sub
    errorHandler:
if Err.Number= Err no corresponds to Claims followup.xlsm is not open
    MsgBox "Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write"
elseif Err.Number = Err no corresponds to Solicitation Number is written correctly
        MsgBox "Please make sure that the Claims followup file is open." & Chr(10) & Chr(10) & "If the file is open make sure that you the Solicitation Number is written correctly."    
elseif Err.Number = Err no corresponds to Claim followup file is in -read only- mode
        MsgBox "Your Claim followup file is in -read only- mode. Your changes may not be saved"

    End Sub

希望这有帮助!

答案 2 :(得分:0)

首先尝试避免错误。对于意外错误,请在错误处理程序中使用Select...Case并让代码在同一位置退出该过程,以便执行所有必需的整理。

我添加了一些检查文件已打开或存在的功能。

Sub some_sub()

    Dim x As Long
    Dim y As Long
    Dim wrkBk As Workbook
    Dim wrkSht As Workbook
    Dim sPath As String, sFile As String

    On Error GoTo ErrorHandler

        sPath = "C:\"
        sFile = "Claims followup.xlsm"

        'Avoid error by checking file exists and is open.
        If FileExists(sPath & sFile) Then

            'Avoided Error1 & 2 completely by checking if the file is open first.
            If WorkBookIsOpen(sPath & sFile) Then
                Set wrkBk = Workbooks(sFile)
            Else
                Set wrkBk = Workbooks.Open(sPath & sFile)
            End If

            'Avoid error 3 by checking if it's read only first.
            If Not wrkBk.ReadOnly Then

                'These all throw errors.
                x = "A"
                y = 0
                Debug.Print 20 / y

                Set wrkSht = ThisWorkbook.Worksheets("Non Existent Worksheet")

            End If

        End If

            On Error GoTo 0

SingleExitPoint:
    'Tidy up, close connections, etc....

Exit Sub

ErrorHandler:
    Select Case Err.Number
        Case 13 'Type Mismatch
            x = 0
            Resume Next 'Continue on the line following the error.
        Case 11 'Division by zero
            y = 2
            Resume 'Continue on the line that caused the error.
        Case 9 'Subscript out of range
            Resume SingleExitPoint

    End Select

End Sub

Public Function WorkBookIsOpen(FullFilePath As String) As Boolean

    Dim ff As Long

    On Error Resume Next

    ff = FreeFile()
    Open FullFilePath For Input Lock Read As #ff
    Close ff
    WorkBookIsOpen = (Err.Number <> 0)

    On Error GoTo 0

End Function

Public Function FileExists(ByVal FileName As String) As Boolean
    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    FileExists = oFSO.FileExists(FileName)
End Function