某些列为空时删除行

时间:2013-10-27 19:30:41

标签: vba excel-vba excel

这个代码有什么问题吗?当列A到F为空时,它应该删除行。谢谢

Sub range_reporter()
    Dim n As Long
    Dim nLastRow As Long
    Dim nFirstRow As Long
    ActiveSheet.UsedRange
    Set r = ActiveSheet.UsedRange
    nLastRow = r.Rows.Count + r.Row - 1
    nFirstRow = r.Row
    For n = nLastRow To nFirstRow Step -1
        If Cells(n, "A") & Cells(n, "B") & Cells(n, "C") & Cells(n, "D") & Cells(n, "E") & Cells(n, "F") = "" Then
            Cells(n, "A").EntireRow.Delete
        End If
    Next n
End Sub 

1 个答案:

答案 0 :(得分:0)

您的问题是您使用了“AND”的不正确语法(“&”字符用于连接)。看看下面的代码,我刚测试它,它工作正常。

Sub range_reporter()
    Dim n As Long
    Dim nLastRow As Long
    Dim nFirstRow As Long

    ActiveSheet.UsedRange

    Set r = ActiveSheet.UsedRange
    nLastRow = r.Rows.Count + r.Row - 1
    nFirstRow = r.Row

    For n = nLastRow To nFirstRow Step -1
        If Cells(n, "A") = "" And Cells(n, "B") = "" And Cells(n, "C") = "" And Cells(n, "D") = "" And Cells(n, "E") = "" And Cells(n, "F") = "" Then
            Cells(n, "A").EntireRow.Delete
        End If
    Next n

End Sub