VBA缓慢进程根据条件去除行

我有一个VBA Excel代码与检查特定列中的值。 如果该列中的行包含值“删除”,然后删除该行。

代码运行良好,但速度很慢。 任何想法如何让代码运行速度更快?

Dim rng1 As Range Dim i As Integer, counter As Integer 'Set the range to evaluate to rng. Set rng1 = Range("g1:g1000") 'initialize i to 1 i = 1 'Loop for a count of 1 to the number of rows 'in the range that you want to evaluate. For counter = 1 To rng1.Rows.Count 'If cell i in the range1 contains an "Delete" 'delete the row. 'Else increment i If rng1.Cells(i) = "Delete" Then rng1.Cells(i).EntireRow.Delete Else i = i + 1 End If Next 

谢谢

C。

 Sub deletingroutine() Dim r As Range For Each r In Range("g1:g1000") If r = "delete" Then r.EntireRow.Delete Next r End Sub 

我设法find一个自动过滤function的解决scheme。

希望它可以帮助别人

  Selection.AutoFilter Set ws = ActiveWorkbook.Sheets("UploadSummary") lastRow = ws.Range("G" & ws.Rows.Count).End(xlUp).Row Set rng = ws.Range("G1:G" & lastRow) ' filter and delete all but header row With rng .AutoFilter Field:=7, Criteria1:="delete" ' 7 refers to the 7th column .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With 

尝试对行进行sorting(在列G上),然后在一个操作中删除所有标记的(“删除”)行。 这要快得多。