Writing an Error Handler

A common convention is to add the error handler code at the end of the procedure, before the End Function or End Sub statement. Add an Exit Sub or Exit Function statement immediately before the error handler line label, to prevent the error handler from running when no error has occurred.

To branch within the error handler, use the Err function, which returns the error number, in conjunction with either the Select Case statement or the If...Then...Else statement. The following section includes an example of this technique.