简体   繁体   中英

delete rows with 2 columns containing specific data

I have an excel file containing one sheet which this data:

      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

I do want to delete all rows if the value from C AND D is 0. How can I achieve this?

Consider:

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

Note:

  • the loop runs bottom-up

be sure you have first row as header one and then use this:

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

将过滤器应用于值0的列C和D,并删除显示的行。

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