3

When handling errors within VBA code, there are a few methods commonly utilized. One of these methods is to use in-line error handling with On Error Resume Next. However, I found it unclear after reading the existing question "VBA: How long does On Error Resume Next work?" if the program continued execute on the very next line or if the program would continue executing at the next logical point.

MSDN has the following description for the functionality of On Error Resume Next:

Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues. Use this form rather than On Error GoTo when accessing objects.

Additionally, there was (now retired) StackOverflow Documentation on the Resume keyword which states something similar for On Error Resume Next:

Resume Next continues execution on the statement immediately following the statement that caused the error. If the error isn't actually handled before doing that, then execution is permitted to continue with potentially invalid data, which may result in logical errors and unexpected behavior.

This does not clearly state how the error handling line handles errors in the initial line of control flow statements. Specifically, if the error occurs in the first line of an If .. Then .. Else .. End If statement, will the program run starting on the first line inside the If statement or will the program run starting on the first line after the End If statement?

Community
  • 1
  • 1
Fritz
  • 624
  • 1
  • 7
  • 14

1 Answers1

5

On Error Resume Next will cause a program to proceed into an If .. Then statement or a [While/Do/For/For Each] loop when the qualifying statement results in an error.

To solve this question I created a pair of test functions in an empty Excel (2007) workbook. I then used the spreadsheet of Excel to evaluate the functions for known error cases and known successful cases.


Test1 - If Statements

Public Function Test1(intN As Integer) As String
On Error Resume Next
    Test1 = ""
    If 1 / intN > 0 Then
        Test1 = Test1 + "A"
    Else
        Test1 = Test1 + "B"
    End If
    Test1 = Test1 + "C"
End Function

This function will throw an error at the line If 1 / intN > 0 Then when the input number is 0, and will evaluate for other numbers such as 1 and -1.

Results:

Test1(-1) -> BC
Test1(0)  -> AC
Test1(1)  -> AC

As seen in the results, the error in Test1(0) caused the program to skip the line If 1 / intN > 0 Then. A was added to the string, and then the program skipped from Else to End If. The character C was added to the string and the function ended.


Test2 - [Do] Loops

Public Function Test2(intN As Integer) As String
On Error Resume Next
    Test2 = ""
    Do While 1 / intN > 0
        Test2 = Test2 + "A"
        intN = intN - 1
    Loop
    Test2 = Test2 + "C"
End Function

This function will throw an error at the line Do While 1 / intN > 0 when the input number is 0, will return a string of AA..AC for positive integers, and return C for negative integers.

Results:

Test1(-1) -> C
Test1(0)  -> AC
Test1(1)  -> AAC

When entering a loop, On Error Resume Next will cause the program to skip from Do While 1 / intN > 0 directly to Test2 = Test2 + "A", regardless of which iteration of the loop it is in.

Whenever intN = 0, the program ran through the code contained in the loop and hit intN = intN - 1, causing intN = -1 and not going through the loop code again.

From this, it can be interpreted that in the case of a For or While loop, the loop would operate in a similar manner, taking the literal next line and going back when hitting the bottom of a loop.

Fritz
  • 624
  • 1
  • 7
  • 14