Skip to content

Errors

StepDescription
On Error GoTo LabelSets the error handler label; redirects to this label if a runtime error occurs.
Normal code executionRuns code normally until an error occurs or procedure ends (via Exit Sub/Function).
Error occursExecution jumps immediately to the error handler label.
Error Handler blockExecutes error handling code (e.g., logging, cleanup, user messages).
Resume statementsControl flow options after error handling:
- ResumeRe-execute the error-causing line (retry).
- Resume NextSkip the error line; continue with the next statement.
- Resume Jump to a specific label (often cleanup) before continuing or exiting.
Exit Sub/Function before handlerPrevents normal code from running into the error handler block accidentally.
Cleanup code (optional)Frees resources or resets state before exiting or continuing.
End of procedureWhen no error or after error handling with no resume, execution ends at End Sub/Function.

This module contains a general error handler.

Usage:

After declarations, add this

On Error GoTo Error_Handler

Add the module code and then

Cleanup:
Set openObject = Nothing
Exit Function
Error_Handler:
GeneralErrorHandler err, Me.Name, "ReplaceWithProcedureName"
Resume Cleanup
End Function

Or for a subroutine

Cleanup:
Set openObject = Nothing
Exit Sub
Error_Handler:
GeneralErrorHandler err, Me.Name, "ReplaceWithProcedureName"
Resume Cleanup
End Sub

Cleanup with object

Cleanup:
Set openObject = Nothing
Exit Sub

Cleanup without object

Cleanup:
Exit Sub

From error

Error_Handler:
GeneralErrorHandler err, Me.Name, "ReplaceWithProcedureName"
Resume Cleanup

Module error

Error_Handler:
GeneralErrorHandler err, MODULE_NAME, "ReplaceWithProcedureName"
Resume Cleanup
ArgumentDescription
variant (required)The value to check for Null (can be a field, control, variable).
valueifnull (optional)The value to return if variant is Null. Defaults to 0 for numbers, "" for strings (but specifying it explicitly is best).
  • Returns variant if not Null.
  • Returns valueifnull if variant is Null.
  • Useful to avoid runtime errors when Null values might occur.
  • Best practice: always specify valueifnull, especially for strings, to ensure consistent results.