我一直在使用这个网站,并且一直在寻找一种相对简单的方法来在Excel 07中实现VBA中的任务。
我有一个列有很多不同的值,我试图在AA:AA中找到以“L-”开头的单元格,然后从那里删除表格中的行。 activesheet / activebook永远不会改变,但我遇到的问题是该列偶尔会有空白单元格。我尝试过使用下面的代码,但是我没有达到我需要的结果。
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
答案 0 :(得分:1)
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
答案 1 :(得分:0)
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