I am attempting to retrieve the number (count) of "#N/A" and "#Value" cells within a range (EG A1:A100).
So far I have 2 solutions:
the solution works but lags a dynamic element. If it does update, it is slow and late to the party.
Public Function ErrorArray(Rng As Range)
Dim ErrorCount As Integer
Dim Cell As Range
Application.Volatile
For Each Cell in Rng
If Cell.Errors.Items(xlEvaluateToError) = True Then
ErrorCount = ErrorCount + 1
End If
Next Cell
ErrorArray = ErrorCount
End Function
*Please excuse any errors, it did work so thats not the point.
The issue with this solution is the massive drop in workbook performance. Does anyone else know an efficient and dynamic solution, either formula or code?
NOTE: *The following function will work if called from a vba Macro (Sub
). The .SpecialCells
method doesn't seem to work when called from a worksheet via a UDF`
Public Function ErrorArray(Rng As Range) As Long
Dim c As Range
Set c = Rng.SpecialCells(xlCellTypeFormulas, xlErrors)
If Not c Is Nothing Then
ErrorArray = c.Count
Else
ErrorArray = 0
End If
End Function
If you just want to count only the #N/A
and #VALUE!
errors:
Public Function ErrorArray(Rng As Range) As Long
Dim r As Range, c As Range
Dim errCount As Long
Set r = Rng.SpecialCells(xlCellTypeFormulas, xlErrors)
If Not r Is Nothing Then
For Each c In r
'Debug.Print c.Text, c
Select Case c
Case CVErr(2042), CVErr(2015)
errCount = errCount + 1
End Select
Next c
Else
errCount = 0
End If
ErrorArray = errCount
End Function
If you have very large numbers of errors, the above code can be sped up by using variant arrays. Let me know.