简体   繁体   中英

Excel VBA find #N/A

I have a worksheet with a formula that returns =NA() under certain conditions. Using VBA, I'd like to find #N/A, but I haven't been able to tweak this answer .

lastrow = .Cells.Find(What:="*", _
                  After:=.Range("A1"), _
                  Lookat:=xlPart, _
                  LookIn:=xlFormulas, _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlPrevious, _
                  MatchCase:=False).Row

I've tried What:=CVErr(xlErrNA) , What:=xlErrNA and also What:="#N/A" to no avail.

As an added difficulty, I have to take Dutch into account, so What:="#N/A" probably wouldn't even work in Dutch.

Note. I'm asking this question out of curiosity as I haven't found a method online. At this moment, I'm calculating which cells contain =NA()

You're looking in the cell formulas. Try looking in the cell values , then I can get this to work using the string "#N/A" for the What argument:

lastrow = .Cells.Find(What:="#N/A", _
            After:=.Range("A1"), _
            Lookat:=xlPart, _
            LookIn:=xlValues, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row

Please try the below code , You should use the loop to read the error.

Sub Test()

If Range("A2").Text = "#N/A" Then
    MsgBox "hi"
End If

End Sub

Hi I have another solution, You have paste these formulas values into another columns as text and

then use the replace code,

Please try the below code.

Sub Tst()

    Columns("C:C").Select
    Selection.Copy
    Range("D1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Selection.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

End Sub

The column C Contains formulas values (Error) .

The problem here is that you're looking for "#N/A" as a value of the cell and it's not the value it's a error indicator, so if you're trying to find a cell wich gives you a error you have to use something like this:

If WorksheetFunction.IfError("Put here a loop to read the cells;"Error")="Error" then

"Write what you desire for cells with error"

end if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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