VBA删除行

我想写一个Excel的VBA代码,删除基于某些单元格的内容的行(都在同一列)。 我的程序现在看起来像这样:

Sub removeCol() Dim i As Integer i = 1 Do While i < 10 If (Cells(i, 2).Text = "a") Then Rows(i).Delete i = i + 1 Else i = i + 1 End If Loop 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 

其中一个变体是:

 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 

如果你想使用contains方法检查单元格内容,那么你可以使用这个:

 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 

或这个:

 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