如果我有大约十万行,如何替换LOOP

时间:2014-05-07 14:19:55

标签: excel vba excel-vba

我有一个很好的宏,但它超级慢,见下文。它基本上做的是宏遍历F列中的每一行,而不是删除该行。

我可以更快地用更快的东西替换我的循环吗?

非常感谢,并向捷克共和国致以最诚挚的问候。

Sub Delete2_Find()
    Dim rgFoundCell As Range

    Application.ScreenUpdating = False
    With Sheets("Raw Data")
        Set rgFoundCell = .Range("F:F").Find(what:=Month(Now) - 2)
        Do Until rgFoundCell Is Nothing
            rgFoundCell.EntireRow.Delete
            Set rgFoundCell = .Range("F:F").FindNext
        Loop
    End With
    Application.ScreenUpdating = True


MsgBox "DONE!"
End Sub

1 个答案:

答案 0 :(得分:2)

你能否联合范围并立即将其删除?这有用吗?像这样:

Option Explicit

Sub Delete2_Find()
    Dim rgFoundCell As Range
    Dim toBeDeted As Range
    Dim firstAddress

    Application.ScreenUpdating = False

    With Sheets("Raw Data").Range("F:F")
        Set rgFoundCell = .Find(what:=Month(Now) - 2)

        If Not rgFoundCell Is Nothing Then
            firstAddress = rgFoundCell.Address

            Do
                If toBeDeted Is Nothing Then
                    Set toBeDeted = rgFoundCell.EntireRow
                Else
                    Set toBeDeted = Union(toBeDeted, rgFoundCell.EntireRow)
                End If

                Set rgFoundCell = .FindNext(rgFoundCell)
                If rgFoundCell Is Nothing Then Exit Do

            Loop While rgFoundCell.Address <> firstAddress

        End If
    End With

    Application.ScreenUpdating = True

    If Not toBeDeted Is Nothing Then _
        toBeDeted.Select ' Delete

    MsgBox "DONE!"
End Sub