检查大量单元格的最有效的技术包含错误?

我有一个电子表格,其中包含大量的请求数据的函数调用。 我正在写一个函数(在VBA中)来检查是否有任何单元格包含错误值“#VALUE”等

此刻,我逐行,逐列迭代,首先检查单元格是否包含公式,如果是,则检查instr为“#VALUE”,“#N / A”等

但是,我想知道是否会更快地模拟单击excel中的整个列,然后“ctrl + f”的值在VBA中。

什么是最有效的方法? 我正在检查一个27列x 1200行大的工作表。

编辑我刚刚意识到有一些细胞有“#N / A”,这是因为他们不包含一个特定的公式。 我只需要在包含特定公式的单元格中search….这可能吗?

EDIT2我有效地需要logging一个macros返回resutls,就像“find所有”。 我用“查找”,我可以得到一个布尔值,但“查找所有”不logging任何VBA代码….

您可以使用SpecialCells仅返回包含错误的单元格。

 Sub Demo() Dim sh As Worksheet Dim rng As Range, cl As Range For Each sh In ActiveWorkbook.Worksheets Set rng = Nothing On Error Resume Next Set rng = sh.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors) On Error GoTo 0 If rng Is Nothing Then Debug.Print "No Errors" Else For Each cl In rng If cl.Formula Like "*" Then ' <-- replace * with your criteria Debug.Print cl.Address End If Next End If Next End Sub 

鉴于你想要的最有效的方法,你可以尝试这种方法,避免了一个缓慢的范围循环

  1. 循环通过SpecialCells公式chichi包含错误(根据其他解决scheme)
  2. 使用Find来检测特定的公式,而不是(1)中的每个单元格的简单循环,

这段代码使用R1C1方法进入Find所以如果需要的话,代码改变这个Application设置(然后返回到最后)

我build议你logging下你想要find的公式,然后input这个公式R1C1表示法的一大优点是它不受实际行和列位置的影响。

例如在A1表示法中的一个公式

  • A5 SUM(A1:A4)将需要SUM(B1:B4) in B5`中的SUM(B1:B4) in不同的search
  • R1C1这两个情况下=SUM(R[-4]C:R[-1]C)

 Sub Demo() Dim ws As Worksheet Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range Dim strAddress As String Dim bRefSTyle If Application.ReferenceStyle = xlA1 Then Application.ReferenceStyle = xlR1C1 bRefSTyle = True End If For Each ws In ActiveWorkbook.Worksheets Set rng1 = Nothing On Error Resume Next Set rng1 = ws.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors) On Error GoTo 0 If rng1 Is Nothing Then Debug.Print ws.Name & ": No Formulae errors" Else 'search errors for particular formula 'this sample looks for a formula which SUMS the four cells directly above it Set rng2 = rng1.Find("=SUM(R[-4]C:R[-1]C)", , xlFormulas, xlWhole) If Not rng2 Is Nothing Then strAddress = rng2.Address Set rng3 = rng2 Do Set rng2 = rng1.Find("=SUM(R[-4]C:R[-1]C)", rng2, xlFormulas, xlWhole) Set rng3 = Union(rng2, rng3) Loop While strAddress <> rng2.Address Debug.Print ws.Name & ": " & rng3.Address Else Debug.Print ws.Name & ": error cells, but no formulae match" End If End If Next 'restore styles if necessary If bRefSTyle Then Application.ReferenceStyle = xlA1 End Sub