Excel VBA查找具有公式结果#N/A的行[已关闭]

v8wbuo2f  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(130)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question

如何使用vba从结果#N/A中获取行?

4ktjp1zp

4ktjp1zp1#

您可以使用SpecialCells获取所有有错误的公式:

Dim errorCells as Range, cell as Range
On Error Resume Next    ' Prevent Runtime error when no errors found
Set errorCells = ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error Goto 0
If Not errorCells is Nothing then
    For Each cell in errorCells
        Debug.Print cell.Address, cell.Formula
    Next 
End If
7rtdyuoh

7rtdyuoh2#

示例:在消息框中显示出错的行

Sub GetRow()
Dim i As Long, lastRow As Long
Dim colG
Dim errRows As String
Dim shtDebet As Worksheet: Set shtDebet = ThisWorkbook.Worksheets("Debet")

lastRow = shtDebet.Cells(Rows.Count, "G").End(xlUp).Row
colG = shtDebet.Range("G1:G" & lastRow).Value

For i = 1 To UBound(colG)
    If IsError(colG(i, 1)) Then
        errRows = errRows & "," & i'append the row with error
    End If
Next i
errRows = Mid(errRows, 2)
MsgBox "These are the rows with errors - " & errRows
End Sub

相关问题