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:
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
Sub removeCol()
Dim i As Integer
For i = 10 To 1 Step -1
If Cells(i, 2) = "a" Then
Rows(i).Delete
End If
Next
End Sub