1
Sub Macro9()
Dim LReturnValue As Boolean

LReturnValue = IsError(Sheets("Lookup Addition").Range("A:A").Value)

If LReturnValue = False Then
    i = MsgBox("there were no errors", vbOKOnly)
Else
    i = MsgBox("there were errors", vbOKOnly)
End If

End Sub

我对 IsError(Customfunction()) 语法应该是什么感到有些困惑。我们如何告诉它检查范围内的每个单元格?

4

2 回答 2

4

计算范围内的错误不需要循环(如果范围很大,这可能会非常慢)甚至任何 VBA。

只需将此工作表函数添加到某个单元格中即可。如果您不希望用户看到此单元格,您可以隐藏行/列/工作表。

=SUMPRODUCT(ISERROR(A:A)*(1=1))

如果您仍然想要用户的弹出框,您的 VBA 现在将是:

Sub CountErr()  
     MsgBox "There are " & ActiveSheet.Range("B1").Value & " Errors"  
End Sub

说得通?

于 2013-04-25T15:22:20.690 回答
1

您可以简单地使用Evaluate和工作表函数COUNTIF来计算错误数:

Sub CheckRangeForErrors()

    Dim errCount As Long
    Dim rng As Range
    Dim cl As Range
    Dim col As String

    col = Application.InputBox("Enter the column letter you would like to check for errors", "Column Name?")

    If Not Len(col) = 1 Then
        MsgBox "You have entered an invalid selection", vbCritical
        Exit Sub
    End If

    Set rng = Sheets("Lookup Addition").Range(col & "1", Range(col & "1048576").End(xlUp))

    errCount = Application.Evaluate("COUNTIF("& rng.Address &",IsError)")

    If errCount = 0 Then
        MsgBox "there were no errors", vbOKOnly
    Else
        MsgBox "there were " & errCount & " errors", vbOKOnly
    End If


End Sub
于 2013-04-25T15:15:43.623 回答