ErrorCheckingOptions
Excel offers Error checking options to trap worksheet errors like Empty Cells Error, Evaluate to error, Inconsistent formula error etc. These errors can be captured by enabling ErrorcheckingOptions in Excel by following below steps or using VBA (Visual Basic for Applications)
Enable Error Checking
- Navigate to File menu
- Click on Options
- Navigate Formula tab
- Check necessary Error checking rules as shown below like you can change color for default error in the sheet
Error Rules

Properties
All error options can be trapped from Errors collection object exposed by Range object by iterating Item method which takes a valid constant against each error.
BackgroundChecking
If cell formula pointing to empty cell reference the AutoCorrect option appears in Excel
Code example
Public Sub ErrorBackgroundCheckingOptionsExample() 'Enable background error checking Application.ErrorCheckingOptions.BackgroundChecking = True End Sub
Output

EmptyCellReferences
It is similar to Backgroundchecking property but shows Empty cell error under AutoCorrect popup.
Code example
Public Sub EmptyCellReferencesCodeExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable empty cell reference Application.ErrorCheckingOptions.EmptyCellReferences = True If oRange.Errors.Item(xlEmptyCellReferences).Value = True Then Debug.Print "Empty cell reference is on" End If 'Memory cleanup Set oRange = Nothing End Sub
Output

EvaluateToError
Excel engine evaluate formulas for the cells containing error. This property enables AutoCorrect option as shown below when set to True.
Code example
Public Sub EvaluateToErrorCodeExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable background error checking Application.ErrorCheckingOptions.EvaluateToError = True 'Check evaluate to error option If oRange.Errors.Item(xlEvaluateToError).Value = True Then Debug.Print "Evaluate to Error cell reference is on" End If 'Memory cleanup Set oRange = Nothing End Sub

InconsistentFormula
This property helps identifying inconsistency within formulas if sets to True
Public Sub InconsistentFormulaCodeExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable background error checking Application.ErrorCheckingOptions.InconsistentFormula = True 'Check evaluate to error option If oRange.Errors.Item(xlInconsistentFormula).Value = True Then Debug.Print "InconsistentFormula cell reference is on" End If 'Memory cleanup Set oRange = Nothing End Sub
Output


IndicatorColorIndex
Takes two constants xlColorIndexAutomatic which indicates default behavior or error checking color indicator and xlColorIndexNone which indicates not color index set.
Code example
Public Sub IndicatorColorIndexCodeExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Validate error checking options color indicator If Application.ErrorCheckingOptions.IndicatorColorIndex = xlColorIndexAutomatic Then Debug.Print "Error checking color indicator set to Automatic mode" Else Debug.Print "Error checking color indicator not set" End If 'Memory cleanup Set oRange = Nothing End Sub
NumberAsText
This property helps identifying numbers which are punched as text within a cell when sets to True and enables AutoCorrect popup as shown below
Code example
Public Sub NumberAsTextCodeExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable number as text error checking option Application.ErrorCheckingOptions.NumberAsText = True 'Validate error checking options Number as text If oRange.Errors.Item(xlNumberAsText).Value = True Then Debug.Print "Error checking Number as text is On" Else Debug.Print "Error checking Number as text is Off" End If 'Memory cleanup Set oRange = Nothing End Sub
Output

OmittedCells
Cells which contain such formulas pointing to a range that excludes close or adjacent cells which possibly can be included, AutoCorrect popup gets enabled to help user as shown below where we are doing SUM over B2:B4 and left B5 hence Sum cell shows Omitted Cells error
Code example
Public Sub OmittedCellsCodeExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable Omitted Cells error checking option Application.ErrorCheckingOptions.OmittedCells = True 'Validate error checking options Omitted Cells If oRange.Errors.Item(xlOmittedCells).Value = True Then Debug.Print "Error checking Omitted Cells is On" Else Debug.Print "Error checking Omitted Cells is Off" End If 'Memory cleanup Set oRange = Nothing End Sub
Output

TextDate
This property enables error checking for two digits year within a cell having date when sets to True and enables AutoCorrect popup.
Code example
Public Sub TextDateExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable Text Date error checking Application.ErrorCheckingOptions.TextDate = True 'Check two digits year If oRange.Errors.Item(xlTextDate).Value = True Then Debug.Print "Text Date error check is on" End If 'Memory cleanup Set oRange = Nothing End Sub
Output

UnlockedFormulaCells
This property enables check for unlocked cells having formula when sets to True, enables AutoCorrect popup as shown below in output
Code example
Public Sub UnlockedFormulaCellsExample() 'Declare range object Dim oRange As Range 'Bind selection reference object Set oRange = Selection 'Enable Text Date error checking Application.ErrorCheckingOptions.UnlockedFormulaCells = True 'Lock the cell oRange.Locked = False 'Memory cleanup Set oRange = Nothing End Sub
Output

Note: ErrorCheckingOptions object exposed by Application object and most of the properties are boolean type
VBA Error object
Trapping error in VBA depends on the developer what approach he decides. VBA offers two types of error trapping:
- In-Line: this trapping trick is used before code execution where user suspects that given statement may result to certain error. Mostly common error trapping which can be used as:
- On Error Resume Next
- Procedure or function level: this error mechanism implements error trapping on entire code block written under a procedure or function using GoTo statement which requires a label or Zero. Commonly used statement as given below which raise a default error determined by VBA compiler:
- On Error GoTo 0
- Centralize Error trapping: Create a common function and call it through all the modules to trap the error
Procedure/Function Error
Public Sub ProcedureLevelErrorTrapping() On Error GoTo myError 'code to be executed 'Define lable myError: 'Define Error If Err.Number <> 0 Then Debug.Print Err.Description End If End Sub
In above code we have used “myError” label hence whenever code execution encounters an error the control will shift to myError label and evaluate the error. A label must be attached by : (double colon)
Err.Raise
Allows developer to raise own error or customize error as shown below.
Syntax
Err.Raise(Number, Source, Description, HelpFile, HelpContext
Code example
Public Sub ErrorRaiseExample() Dim A As Integer Dim B As Integer A = 5 B = 5 'Check if A is equal to B If A = B Then Err.Raise Number:=123, Description:="A cannot be equal to B" End If End Sub
Output

Err.Clear
Some times uses may require to continue execution exceptionally, the Clear method will allow user to clear or suppress the error.
Code example
Public Sub ClearErrorExample() On Error GoTo myError 'code to be executed 'Define lable myError: 'Define Error If Err.Number <> 0 Then 'Clear error Err.Clear End If
Err.Number
Each error in VBA has a unique number to be identified. User can trap errors with the help of Error number and take necessary action as shown below where we try to perform division between Integer and String and Error Number 13 would be generated by compiler which is known for Type Mismatch.
Code example
Public Sub ErrorNumberExample() On Error GoTo myError Dim a As Integer Dim b As String a = 5 b = "k" Debug.Print a / b 'Define lable myError: 'Define Error If Err.Number = 13 Then MsgBox "You are trying to perform operation between cross data types" End If End Sub
Output

There are various properties offered by Err object in VBA (Visual Basic for Applications) as listed below:
- Description
- HelpContext
- HelpFile
- LastDllError
- Source