根据另一个工作表中的单元格加快删除日期之间的行

时间:2018-06-27 19:13:27

标签: excel vba excel-vba

我有一个正常的例程,但是似乎需要很长时间。基本上,我想删除工作表“数据”列C中不属于我在工作表“仪表板”中输入的两个日期范围的开始日期H13和结束日期H14

Sub dateDelete()
Sheets("Data").Select
Const TEST_COLUMN As String = "C"    '<=== change to suit
Dim i As Long
Dim lastRow As Long
Dim dteStart As Date
Dim dteEnd As Date

    dteStart = Worksheets("Dashboard").Range("H13")
    dteEnd = Worksheets("Dashboard").Range("H14")

    With Application
    '    .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With

    With ActiveSheet

        lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        For i = lastRow To 2 Step -1

            If .Cells(i, "C").Value < dteStart Or .Cells(i, "C").Value > dteEnd Then

                .Rows(i).Delete
            End If
        Next i

    End With

    With Application

        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With

End Sub

1 个答案:

答案 0 :(得分:0)

使用自动过滤器识别要删除的行。

With ActiveSheet
    if .autofiltermode then .autofiltermode= false
    with .range(.cells(1, "C"), .Cells(.Rows.Count, "C").End(xlUp))
        .autofilter field:=1, criteria1:="<"&dteStart, criteria2:=">"&dteEnd , operator:=xlor
        with .resize(.rows.count-1, 1).offset(1, 0)
            if cbool(application.subtotal(103, .cells)) then
                .cells.entirerow.delete
            end if
        end with
    end with
    if .autofiltermode then .autofiltermode= false
end with