我正在尝试为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
我会非常感谢任何提示!现在这件事根本没有任何影响。
答案 0 :(得分:3)
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
答案 1 :(得分:2)
其中一个变种是:
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