在单元格中定位特定值并删除标识的行

我一直在使用这个网站,并且一直在寻找一种相对简单的方法来在Excel 07中实现VBA中的任务。

我有一个列有许多不同的值,我试图find单元格AA:AA开始“L-”,然后从那里删除表中的行。 activesheet /活动手册永远不会改变,但我遇到的问题是列有空白单元格时不时。 我试过使用下面的代码,但我没有达到我需要的结果。

Sub Remove_Expendables() Application.ScreenUpdating = FALSE Range("AA1").Select Do ActiveCell.Offset(1, 0).Select If ActiveCell = (Left("L-",2) Then ActiveCell.ClearContents Loop Until IsEmpty(ActiveCell.Offset(0, 2)) ActiveSheet.Columns("AA:AA").SpecialCells(xlCellTypeBlanks).EntireRow.Delete Application.ScreenUpdating = TRUE End Sub 

 Sub Remove_Expendables() Dim c as range, rngDel as range set c= activesheet.cells(rows.count, "AA").end(xlup) Application.ScreenUpdating = FALSE Do While c.row > 1 If c.value like "L-*" Then if rngDel is nothing then set rngDel=c else set rngDel=application.union(rngDel,c) end if end if set c=c.offset(-1,0) Loop if not rngDel is nothing then rngDel.entirerow.delete Application.ScreenUpdating = TRUE End Sub 

AutoFilter比循环快得多。

 Sub AF_Delete() Application.ScreenUpdating = False With ActiveSheet .AutoFilterMode = False .Columns("AA").AutoFilter Field:=1, Criteria1:="=L-*" .AutoFilter.Range.Offset(1, 0).EntireRow.Delete ' .AutoFilterMode = False End With Application.ScreenUpdating = True End Sub