删除包含2列包含特定数据的行

时间:2016-12-21 13:49:48

标签: excel vba macros

我有一个excel文件,其中包含一张此数据的表格:

      A    |     B      |     C    |       D
________________________________________________
    321        2016/12/01    0           0
    123        2016/12/03    23          0
    1321       2016/12/05    12          1
    2315       2015/12/03    0           0
    23154      2015/12/03    0           0

如果C AND D的值为0,我确实要删除所有行。如何实现此目的?

4 个答案:

答案 0 :(得分:1)

考虑:

Sub RowKiller213()
    Dim i As Long, N As Long

    N = Cells(Rows.Count, "A").End(xlUp).Row
    For i = N To 1 Step -1
        If Cells(i, "C").Value = 0 And Cells(i, "D").Value = 0 Then
            Cells(i, "C").EntireRow.Delete
        End If
    Next i
End Sub

<强> 注意:

  • 循环自下而上

答案 1 :(得分:1)

确保第一行作为标题,然后使用:

Sub Main()
    With Worksheets("mySheetName")
        With .Range("D1", .Cells(.Rows.Count, 1).End(xlUp))
            .AutoFilter field:=3, Criteria1:=0
            .AutoFilter field:=4, Criteria1:=0
            If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
End Sub

答案 2 :(得分:0)

答案 3 :(得分:0)

将过滤器应用于cols C和D,其中value为0,并删除显示的行。