我正在研究一个对原始SQL数据进行排序和格式化的Excel程序。我有一个宏,它需要一段时间才能遍历数据,我想对其进行优化。
此宏遍历大约3500行数据,并删除与我的参数不匹配的成员资格。
Sub MemDel()
Dim Row As Long
For Row = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 1)
If .Value <> "SILV" _
And .Value <> "AARP" _
And .Value <> "REA" _
And .Value <> "SILVFIT" _
Then _
Rows(Row).EntireRow.Delete
End With
Next Row
End Sub
答案 0 :(得分:3)
以下是您实现@BigBen建议的方法:
Sub MemDel()
Dim ws As Worksheet
Dim rDel As Range
Dim CheckCell As Range
Set ws = ActiveWorkbook.ActiveSheet
For Each CheckCell In ws.Range("A1", ws.Cells(ws.Rows.Count, "A").End(xlUp)).Cells
Select Case CheckCell.Value
Case "SILV", "AARP", "REA", "SILVFIT" 'do nothing
Case Else: If rDel Is Nothing Then Set rDel = CheckCell Else Set rDel = Union(rDel, CheckCell)
End Select
Next CheckCell
If Not rDel Is Nothing Then rDel.EntireRow.Delete
End Sub