Search code examples
excelvba

How can I check if a Multi-Cell Range contains any errors?


How can I use VBA to check if a Range in Excel contains any #N/A or other types of errors? I know how to do it for a single cell:

IsError(Sheets("Main").Range("B1").value

but doing the same for a multi-cell range:

IsError(Sheets("Main").Range("A12:N32").value)

does not pick up an error. If I use the IsError function as a formula in one of the cells of the sheet, it works and picks up the error:

=ISERROR(A12:N32)


Solution

  • Please open a new workbook and try the code below.
    This will write 3 values to column A, then iterate through the column and show a msgbox if an error occurs. Try it!

    Option Explicit
    
    Sub Throw_Errors()
        Dim ws As Worksheet, rng As Range, i&: Set ws = Sheets(1)
        ws.Range("A2") = "=100/0"
        ws.Range("A3") = "=A2/0"
        ws.Range("A4") = "=100/10"
        For i = 2 To ws.Range("A" & Rows.Count).End(xlUp).Row
            Set rng = ws.Range("A" & i)
            If IsError(rng) Then MsgBox "error in row" & rng.Row
            Set rng = Nothing
        Next i
    End Sub
    

    Also check out this answer if you want to learn how to check for different types of errors!