What is the syntax for error handling?

On Error GoTo <Error handler name>
‘normal code here
Exit Sub

<Error handler name>:
‘code here
Resume Next

This entire block of code above is meant to go into a single procedure.

‘example
On Error GoTo ErrorHandler 'Begins the error handling routine
z = 10 / 0 'Divide by ZERO Error Raises
Exit Sub 'exit so does not continue onto the errorhandler again

ErrorHandler: ' Error-handling routine
If Err.Number <> 0 Then '0 means no error
    MsgBox "Error #" & Err.Number & " : " & Chr(10) & Err.Description 'displays the error
End If
Resume Next 'goes to whatever code is after where the error line was

unnamed (3).jpg

Advertisements