Efficient way to delete rows (Multiple criterias) VBA

前端 未结 1 547
萌比男神i
萌比男神i 2021-01-15 11:41

I have a sheet where in Col A there is a String A and Col B consists of String B.

I want to keep rows with the Word \'Begründung\' in Col A and \'Nein\' in Col B.

1条回答
  •  孤街浪徒
    2021-01-15 12:28

    Try this:

    Sub DeleteWithMultipleColumnsCriterias()
        Dim ws As Worksheet
        Dim rng As Range
        Dim lastRow As Long
    
        Set ws = ActiveWorkbook.Sheets("Sheet1")
    
        lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    
        Set rng = ws.Range("A1:B" & lastRow)
    
        ' filter and delete all but header row
        With rng
            .AutoFilter Field:=1, Criteria1:="<>*Begründung*"
            .AutoFilter Field:=2, Criteria1:="<>*Nein*"
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
    
        ' turn off the filters
        ws.AutoFilterMode = False
    End Sub
    

    0 讨论(0)
提交回复
热议问题