How to delete rows in Excel based on criteria using VBA?

前端 未结 4 762
花落未央
花落未央 2020-11-29 12:35

I am currently building a macro to format a sheet of data as well as to remove inapplicable rows of data. Specifically, I am looking to delete rows where Column L = \"ABC\"

4条回答
  •  醉话见心
    2020-11-29 12:54

    Sub test()
    
        Dim bUnion As Boolean
        Dim i As Long, lastrow As Long
        Dim r1 As Range
        Dim v1 As Variant
    
        lastrow = Cells(Rows.Count, 1).End(xlUp).Row
        v1 = ActiveSheet.Range(Cells(1, 12), Cells(lastrow, 27)).Value2
        bUnion = False
    
        For i = 1 To lastrow
            If v1(i, 1) = "ABC" Or v1(i, 16) <> "DEF" Then
                If bUnion Then
                    Set r1 = Union(r1, Cells(i, 1))
                Else
                    Set r1 = Cells(i, 1)
                    bUnion = True
                End If
            End If
        Next i
        r1.EntireRow.Delete
    
    End Sub
    

提交回复
热议问题