如何优化用于排序和格式化SQL数据的宏?

时间:2019-05-09 19:00:01

标签: excel vba

我正在研究一个对原始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

1 个答案:

答案 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