On Error GoTo LineLabel

This statement lets you provide a custom error handler.

Public Sub MySubroutine() 
   On Error GoTo ErrorHandler


   Exit Sub
ErrorHandler:
   Call MsgBox(Err.Number & " - " & Err.Description)
End Sub

The LineLabel must be in the same procedure as the On Error statement.
You should always put this LineLabel at the end of the subroutine.
This section of code between the LineLabel and the End Sub is often referred to as the Error Handling Routine.
You should always include an Exit Sub, Exit Function or Exit Property before your error handling routine to ensure that it is only executed when an error occurs.


Nested Subroutines



Err Object

This does not automatically clear the Err object.
When you have one error handler per subroutine this is not a problem because the Err is reset automatically when execution leaves the subroutine.


© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext