Home > Blockchain >  VBA On Error GoTo suddenly stopped working
VBA On Error GoTo suddenly stopped working

Time:11-10

I have an issue with the error handling in VBA. The code has previously worked, but now suddenly errors are not handled by On Error GoTo statements and instead the code is crashing and giving a pop up with the error message as if On Error GoTo 0 would be active.

Here is an example of how the code structure is:

On Error GoTo logError

For d = 0 To Doclist.Count -1
    On Error GoTo DownloadFailed
    session.findById("wnd[0]/tbar[1]/btn[30]").press
    session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = filepath
    On Error GoTo logError
    ...

DownloadFailed:
    Err.Clear
    On Error GoTo logError
Next d

logError:
    ws1.Cells(1, 7).Value = Err.Description
    Workbooks("Main.xlsm").Save

In the first iteration the On Error GoTo DownloadFailed is working as expected, but after this the code is crashing. The error that I am getting is Run-time error '619'. I saw on some similar post to clear the error with Err.Clear but this did nothing to my code.

In another part of the code I am using On Error Resume Next which at the same time stopped working. As mentioned the code has worked previously so I have no idea what could be wrong.

Does anybody have experience with similar issues and any possible solutions for this?

CodePudding user response:

Error Handling

  • The GoTo keyword is kind of reserved for the error-handling routine so this solution is strict about that.
  • Some contributors argue though that it is OK to use the GoTo keyword to skip a code block in a loop but I don't think it refers to doing it with On Error.
Sub ErrorTest()
    
    Dim LogErrorFound As Boolean
    Dim ErrNum As Long
    On Error GoTo LogError ' start error-handling routine
    
    For d = 0 To Doclist.Count - 1
        On Error Resume Next ' defer error trapping
            session.findById("wnd[0]/tbar[1]/btn[30]").press
            ErrNum = Err.Number
            If ErrNum = 0 Then
                session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = filepath
                ErrNum = Err.Number
            End If
        On Error GoTo LogError ' resume error-handling routine; clears error
        If ErrNum = 0 Then
            '...
        
        'Else ' download failed i.e. 'ErrNum <> 0'; do nothing!?
        End If
    Next d

ProcExit:
    
    If LogErrorFound Then
        On Error Resume Next  ' defer error trapping; avoid endless loop
            ws1.Cells(1, 7).Value = Err.Description
            Workbooks("Main.xlsm").Save
        On Error GoTo 0 ' stop error trapping
        MsgBox "A log error occurred.", vbCritical
    Else
        MsgBox "Finished successfully.", vbInformation
    End If
    
    Exit Sub

LogError: ' continuation of the error-handling routine
    LogErrorFound = True
    Resume ProcExit

End Sub

CodePudding user response:

While Err.Clear does clear the error object, it does not re-enable error handling like Resume or On Error GoTo 0. In order to manually do this, replace Err.Clear with On Error GoTo -1 like this:

On Error GoTo logError

For d = 0 To Doclist.Count -1
    On Error GoTo DownloadFailed
    session.findById("wnd[0]/tbar[1]/btn[30]").press
    session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = filepath
    On Error GoTo logError
    ...

DownloadFailed:
    On Error GoTo -1
    On Error GoTo logError
Next d

logError:
    ws1.Cells(1, 7).Value = Err.Description
    Workbooks("Main.xlsm").Save
  • Related