Question

Is a Resume always required after On Error handling ? Would it cause stack error to skip it by supressing the Resume line in the below example ?

Sub MySub
    on error goto Hell
    DoThis
    DoThat
    Otherstuff
Adios:
    Exit Sub

Hell:
    MsgBox Err.Description, vbCritical, "Error " & Err.Number
    Resume Adios   'is this line required ?
Exit Sub
Was it helpful?

Solution 2

Resume is not required you can call it if you like. Leaving it out of the example above will not cause any problems - the subroutine will just complete at the final Exit Sub.

OTHER TIPS

The Resume statement instructs VBA to resume execution at a specified point in the code. You can use Resume only in an error handling block; any other use will cause an error. Moreover, Resume is the only way, aside from exiting the procedure, to get out of an error handling block. Do not use the Goto statement to direct code execution out of an error handling block. Doing so will cause strange problems with the error handlers.

The Resume statement takes three syntactic form:

Resume
Resume Next
Resume <label>

Used alone, Resume causes execution to resume at the line of code that caused the error. In this case you must ensure that your error handling block fixed the problem that caused the initial error. Otherwise, your code will enter an endless loop, jumping between the line of code that caused the error and the error handling block. The following code attempts to activate a worksheet that does not exist. This causes an error (9 - Subscript Out Of Range), and the code jumps to the error handling block which creates the sheet, correcting the problem, and resumes execution at the line of code that caused the error.

On Error GoTo ErrHandler:
Worksheets("NewSheet").Activate 
Exit Sub

ErrHandler:
If Err.Number = 9 Then
    ' sheet does not exist, so create it
    Worksheets.Add.Name = "NewSheet"
    ' go back to the line of code that caused the problem
    Resume
End If

The second form of Resume is Resume Next . This causes code execution to resume at the line immediately following the line which caused the error. The following code causes an error (11 - Division By Zero) when attempting to set the value of N. The error handling block assigns 1 to the variable N, and then causes execution to resume at the statement after the statement that caused the error.

    On Error GoTo ErrHandler:
    N = 1 / 0
    Debug.Print N
    Exit Sub

    ErrHandler:
    N = 1
    ' go back to the line following the error
    Resume Next

The third form of Resume is Resume <label>: . This causes code execution to resume at a line label. This allows you to skip a section of code if an error occurs. For example,

    On Error GoTo ErrHandler:
    N = 1 / 0

    code that is skipped if an error occurs

    Label1:

    more code to execute

    Exit Sub

    ErrHandler:
    go back to the line at Label1:
    Resume Label1:

All forms of the Resume clear or reset the Err object.

found this on

http://www.cpearson.com/excel/errorhandling.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top