Search notes:

Excel: cell errors

Excel previsions a specific error values in cells:
VBA constant Number Displayed (english) Displayed (german) Caused by
xlErrDiv0 2007 #DIV/0! #DIV/0! division by zero
xlErrNA 2042 #N/A #NV Worksheet functions such as match, lookup etc. don't find value being looked up.
xlErrName 2029 #NAME! #NAME? name of inexisting function, range etc.
xlErrNull 2000 #NULL! #NULL! intersection that returns empty set.
xlErrNum 2036 #NUM! #ZAHL! value to big (or small?) to be stored as double precision floating-point number
xlErrRef 2023 #REF! #BEZUG! referring to a cell that was later deleted
xlErrValue 2015 #VALUE! #WERT! value errors such as trying to add up text values.
TODO: In newer versions of Excel, there seems also to be a #SPILL error.
The following VBA code, when executed in Excel, creates some possible errors:
option explicit

sub main() ' {
    createCellsWithErrors

    dim r as long

    for r = 1 to 9 ' {
        checkIfCellContainsError  r
    next r ' }

    activeSheet.usedRange.columns.autoFit

    cells(11, 11).select

end sub ' }

sub createCellsWithErrors() ' {

'   xlErrDiv0
'   -----------------------------------------
    cells(1, 3) =  42
    cells(1, 4) =   0
    cells(1, 2).formulaR1C1 = "= RC[1] / RC[2]"

'   xlErrNA
'   -----------------------------------------
    cells(2, 3) = "foo"
    cells(2, 4) = "bar"
    cells(2, 5) = "baz"
    cells(2, 6) = "n.a."
    cells(2, 2).formulaR1C1 = "=match(RC[4],RC[1]:RC[3], 0)"

'   xlErrName
'   -----------------------------------------
    cells(3, 2).formulaR1C1 = "= inexistingFunction()"

'   xlErrNull
'   -----------------------------------------
    cells(4, 2).formulaR1C1 = "= sum(RC[1]:RC[2] RC[3]:RC[4])" ' Use intersect operator (space) on two ranges that have no cell in common

'   cells(5, 1) = "xlErrNum"
    cells(5, 3).formulaR1C1 = "= 17.9769313486231 * 1e307"     ' Close to the maximum double precision floating-point value
    cells(5, 2).formulaR1C1 = "= 17.9769313486232 * 1e307"     ' Over the maximum double precision floating-point value

'   xlErrRef
'   -----------------------------------------
    cells(6, 9) = "some value that is going to be deleted"
    cells(6, 2).formulaR1C1 = "= RC9"
    columns(9).delete

'   xlErrValue
'   -----------------------------------------
    cells(7, 2).formulaR1C1 = "= ""foo"" + ""bar"""            ' cannot add two stringsā€¦

'   xlEmptyCellReferences
'   -----------------------------------------
    cells(8, 3).value       = "17"
    cells(8, 2).formulaR1C1 = "= RC[1] + RC[2]"                ' Some referenced cells in the formula are empty

'   xlNumberAsText
'   -----------------------------------------
    cells(9, 2).value       = "'99"                            ' Prepend number with apostrophe


end sub ' }

sub checkIfCellContainsError(r as long) ' {

    dim cell as range : set cell = cells(r, 2)

    dim cellVal as variant
    cellVal = cell.value

    dim errText as string


    if cell.errors(xlEmptyCellReferences    ).value then errText = "xlEmptyCellReferences"
    if cell.errors(xlInconsistentFormula    ).value then errText = "xlInconsistentFormula"
    if cell.errors(xlInconsistentListFormula).value then errText = "xlInconsistentListFormula"
    if cell.errors(xlListDataValidation     ).value then errText = "xlListDataValidation"
    if cell.errors(xlMisleadingFormat       ).value then errText = "xlMisleadingFormat"
    if cell.errors(xlNumberAsText           ).value then errText = "xlNumberAsText"
    if cell.errors(xlOmittedCells           ).value then errText = "xlOmittedCells"
    if cell.errors(xlTextDate               ).value then errText = "xlTextDate"
    if cell.errors(xlUnlockedFormulaCells   ).value then errText = "xlUnlockedFormulaCells"

    if cell.errors(xlEvaluateToError        ).value then ' {
       errText = errText & ", " & "xlEvaluateToError"

       select case cellVal
              case cvErr(xlErrDiv0 ): errText = "xlErrDiv0"
              case cvErr(xlErrNA   ): errText = "xlErrNA"
              case cvErr(xlErrName ): errText = "xlErrName"
              case cvErr(xlErrNull ): errText = "xlErrNull"
              case cvErr(xlErrNum  ): errText = "xlErrNum"
              case cvErr(xlErrRef  ): errText = "xlErrRef"
              case cvErr(xlErrValue): errText = "xlErrValue"
              case else             : errText = "?"
       end select

      if varType(cellVal) <> vbError then ' {
         msgBox "Unepxected: varType(cellVal) <> vbError, but xlEvaluateToError"
      end if ' }

    end if ' }

    cells(r, 1) = errText

end sub ' }
Github repository about-MS-Office-object-model, path: /Excel/Range/cell/errors.vb
This is the worksheet after executing the code:

See also

Its possible to select cells that contain errors with Home -> Editing -> Find & Select -> Go To Special -> Select Formulas (Errors)
The Errors and Error objects of the Excel Object Model.
The function range.showErrors()

Index