2013-04-25 53 views
0
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())語法應該是什麼感到困惑。我們如何告訴它檢查範圍內的每個細胞?VBA查找特定列中的錯誤值

+0

這取決於你想要做什麼。如果在該範圍內有任何**錯誤值,您是否想簡單地返回「True」?無論如何,你將不得不遍歷整個範圍。我會發佈一個答案。 – 2013-04-25 15:07:40

回答

0

你可以簡單地使用興田工作表函數來計算錯誤#:

LReturnValue = Application.Worksheetfunction.CountIf(Range("A:A"),IsError) > 0

我想這應該做到這一點,並會通知你有多少錯誤,在你選擇的範圍內被發現。

REVISED

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 
+0

是這個代碼'Application.Worksheetfunction.CountIf(Range(「A:A」),IsError)'真的在你的機器上工作。播種時我很興奮,但它不起作用......我錯過了什麼? – 2013-04-25 19:13:05

+0

嗯,未經測試。工作表函數'= CountIf(「A:A」,IsError)'工作。我只是假設VB等價物也可以工作。讓我修改! – 2013-04-25 20:22:46

+0

@KazJaw固定。必須使用'Application.Evaluate'並且連接公式。 WOrksheet函數工作正常(在工作表上,但不是來自VBA!)。感謝您的支持。 – 2013-04-25 20:30:16

3

計數誤差的範圍內,不需要循環(其可以是非常慢的,如果範圍大),或者甚至任何VBA。

只需將此工作表函數添加到某個單元格中。如果你不想讓用戶看到這個單元格,你可以隱藏行/列/表單。

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

如果你仍然想爲用戶彈出框,您的VBA現在是:

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

有意義嗎?