VBA delete rows

前端 未结 2 2002
天涯浪人
天涯浪人 2020-12-12 04:55

I am trying to write a VBA code for excel that deletes rows based on the contents of certain cells (all in the same column). My program right now looks like this:

         


        
2条回答
  •  隐瞒了意图╮
    2020-12-12 05:16

    one of the variants is:

    Sub test()
        Dim i&, x&
        i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row 'get last row in "B" column
        For x = i To 1 Step -1
            If LCase(Cells(x, 2).Value2) = "a" Then Rows(x).Delete 'Lcase to remove case sensivity, due to "a" not equal to "A"
        Next x
    End Sub
    

    if you want check cells contents using contains method, then you can use this:

    Sub test2()
        Dim i&, x&
        i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row
        For x = i To 1 Step -1
            If LCase(Cells(x, 2).Value2) Like "*a*" Then Rows(x).Delete
        Next x
    End Sub
    

    or this:

    Sub test3()
        Dim i&, x&
        i = [B:B].Cells.Find("*", , , xlByRows, , xlPrevious).Row
        For x = i To 1 Step -1
            If InStr(1, Cells(x, 2).Value2, "a", vbTextCompare) > 0 Then Rows(x).Delete
        Next x
    End Sub
    

提交回复
热议问题