根据日期删除多行

时间:2019-03-15 15:35:22

标签: excel vba

我创建了一个宏,其中我根据行在“ C”列中的日期删除了每一行。我在处理宏时遇到了麻烦,因为它遍历了每一行并以不连续的方式而不是一次全部删除了它们。有什么办法可以加快该过程,以便它不会花费30分钟来运行。顺便说一下,文件很大。

Sub DeleteRowsBeforeCutoff()
    Application.ScreenUpdating = False

    NumRows = Range("C3", Range("C3").End(xlDown)).Rows.Count
    Range("A1").Select
    For x = 3 To NumRows
        If Cells(x, 3) < [K1] Then
            Cells(x, 3).EntireRow.Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

如@Gary所写,代码将向后循环, 我尝试通过yr代码(更新)将每行删除为25K范围行, 结果是: mycode:00:00:03:123 joecode:00:00:14:1214

两个代码:

Sub testJoe()
Dim startTime As Single
Dim i As Long
startTime = Time()
For i = Range("C2").End(xlDown).Row To 2 Step -1
    If Cells(i, 3) = 3 Then Rows(i).EntireRow.Delete
Next
Debug.Print Format(Time() - startTime, "HH:nn:ss:ms")
End Sub

Sub mytest()
Dim startTime As Single, rang As Range, sh As Worksheet
startTime = Time()
Set sh = ActiveSheet
sh.UsedRange.AutoFilter Field:=3, _
                        Criteria1:="=3", _
                        VisibleDropDown:=False
Set rang = sh.UsedRange.Offset(1, 0)
Set rang = rang.Resize(rang.Rows.Count - 1)
Set rang = rang.SpecialCells(xlCellTypeVisible)
rang.EntireRow.Delete
Selection.AutoFilter
Debug.Print Format(Time() - startTime, "HH:nn:ss:ms")
End Sub