简体   繁体   English

如何检查多单元格范围是否包含任何错误?

[英]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? 如何使用VBA检查Excel中的范围是否包含任何#N / A或其他类型的错误? 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函数用作工作表单元格之一中的公式,它将起作用并拾取错误:

=ISERROR(A12:N32)

Thanks in advanced. 提前致谢。

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. 这会将3个值写入列A,然后遍历该列并在发生错误时显示msgbox。 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! 如果您想学习如何检查不同类型的错误,也请查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM