Check for the current error handling in VBA

Viewed 56

In VBA error handling is done by on error statement.

I want to temporarily change the error handling and then go back to the previous behavior afterward. How would it be possible to check the current error handling and store it in a variable (I couldn't find anything in the references)?

'set the error handling to s.th. "on error... "
'some code with the regular error handling

'change the error handling to "on error ..." (regardless of what it was before)
'some code with the new error handling

'change back to the previous error handling
'some code with the regular error handling

Background: I needed to do a is nothing check on a Variant array to exclude empty object indexes from being used, but is nothing applied to an array index that holds a value throws an exception, so I temporary wanted to change the error handling to on error resume next. Eventually is solved this using a different approach but I'm still wondering if I can determine the current error handling somehow during runtime Here's the question and answer to my original problem.

EDIT: I know I can check my previous code manually to find out what type of error handling has been used. However I want to avoid that (to save time).

I suppose as a workaround I could set an additional variable with the state which I can then check for the current state, although this will result in quite a bit of overhead. Something like this:

Dim errorHandling as String

errorHandling = "resumeNext"
on error resume next
'some code

'changing the error handling temp.
'some other code

'changing the error handling to it's previous state
if errorhandling = "resumeNext" then
   On Error Resume Next
elseif errorhandling = "GoToErrorhandler" then
   On Error GoTo errorhandler
End If

'Rest of the code
1 Answers

Read/Write to Array

Option Explicit

Sub ReadWriteArrayExample()

    Dim myArray() As Variant: ReDim myArray(1 To 10)
    
    Dim i As Long
    Dim n As Long
    
    ' Fill the array.
    For i = 1 To 10
        n = Application.RandBetween(0, 1)
        If n = 1 Then ' write a random number between 1 and 10 inclusive
            myArray(i) = Application.RandBetween(1, 10)
        'Else ' "n = 0"; leave the element as-is i.e. 'Empty';do nothing
        End If
    Next i
    
    ' Debug.Print the result.
    Debug.Print "Position", "Value"
    For i = 1 To 10
        If Not IsEmpty(myArray(i)) Then ' write the index and the value
            Debug.Print i, myArray(i)
        'Else ' is empty; do nothing
        End If
    Next i

End Sub

Error Handling

Sub ErrorHandling()

    Const ProcName As String = "ErrorHandling"
    On Error GoTo ClearError ' enable error trapping

    ' Some code

    On Error Resume Next ' defer error trapping
        ' Some tricky code
    On Error GoTo ClearError ' re-enable error trapping

    ' Some Code

ProcExit:
    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
    
End Sub
Related