0

我有一个包含 1000 条记录的 excel,如果 C、D 和 E 列具有 #N/A 值,我想删除我的 excel 中的行,我尝试了一些来自论坛的代码,但它没有用。请帮助我。这是示例

一个 | 乙| C | D | E |(列)

1 | 洛杉矶 | 大号 | #不适用 | #不适用

2 | 洛杉矶 | 大号 | #不适用 | 大号

3 | 洛杉矶 | #不适用| #不适用 | 大号

4 | 洛杉矶 | #不适用| #不适用 | #N/A ---- 删除这一行

5 | 文学士 | #不适用 | #不适用 | 大号

6 | 加利福尼亚州 | #不适用 | #不适用 | #N/A -----删除此行

4

2 回答 2

1

Give this a try:

Sub KillNAS()
    Dim N As Long, c As String
    c = "#N/A"
    N = Cells(Rows.Count, 1).End(xlUp).Row
    For i = N To 1 Step -1
        If Cells(i, "C").Text = c And Cells(i, "D").Text = c And Cells(i, "E").Text = c Then
            Cells(i, "C").EntireRow.Delete
        End If
    Next
End Sub
于 2013-08-21T14:42:02.547 回答
1

或者,使用查找循环并在最后一次删除找到的行:

Sub tgr()

    'Declare variables
    Dim rngFound As Range   'Used for the find loop
    Dim rngDel As Range     'Used to store matching rows
    Dim strFirst As String  'Used to store the first cell address of the find loop to prevent infinite loop

    'Search for #N/A in column C
    Set rngFound = Columns("C").Find("#N/A", Cells(Rows.Count, "C"), xlValues, xlWhole)

    'Make sure something was found
    If Not rngFound Is Nothing Then
        'Found something, record first cell address
        strFirst = rngFound.Address

        'Start loop
        Do
            'Check if cells in column C, D, and E are all #N/A
            If Cells(rngFound.Row, "C").Text = "#N/A" _
            And Cells(rngFound.Row, "D").Text = "#N/A" _
            And Cells(rngFound.Row, "E").Text = "#N/A" Then

                'Found they are all #N/A, store the row in rngDel
                If rngDel Is Nothing Then Set rngDel = rngFound Else Set rngDel = Union(rngDel, rngFound)

            End If

            'Advance the loop to the next cell with #N/A in column C
            Set rngFound = Columns("C").Find("#N/A", rngFound, xlValues, xlWhole)

        'Exit loop when back to the first cell
        Loop While rngFound.Address <> strFirst
    End If

    'If rngDel has anything in it, delete all of its rows
    If Not rngDel Is Nothing Then rngDel.EntireRow.Delete

    'Object variable cleanup
    Set rngFound = Nothing
    Set rngDel = Nothing

End Sub
于 2013-08-21T14:55:55.883 回答