小编Red*_*ift的帖子

宏的运行时间越来越长

我的代码工作正常,但问题是运行时间越来越长,每次使用宏时完成计算所需的时间都会增加.我已经尝试过使用sytax进行各种变化和修改,但由于我对VBA很新,所以我没有取得很大的进步.这是我正在运行的代码(注意,它作为子集运行,并且ScreenUpdate = False):

Public Sub deleteRows()

    Dim lastRow As Long
    Dim rng As Range
    With ActiveSheet
        .AutoFilterMode = False
        lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
        '~~> Set the range of interest, no need to include the entire data range
            With .Range("B2:F" & lastRow)
                .AutoFilter Field:=2, Criteria1:="=0.000", Operator:=xlFilterValues
                .AutoFilter Field:=5, Criteria1:="=0.000", Operator:=xlFilterValues
            End With
        .Range("B1:F" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilterMode = False
        Rows("1:1").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    End With
    MsgBox Format(Time - start, "hh:mm:ss")

End Sub
Run Code Online (Sandbox Code Playgroud)

此代码通过删除整行基本上从数据中删除零值结果.最初,它在大约12秒内运行,但很快就变成了55秒,这已经发展到增加的长运行时间,"快速"现在处于5小步范围内.下面是一个电子表格,其中包含已记录的运行时和相应的更改:

Runtime Changes
6:30    None
7:50    None
5:37 …
Run Code Online (Sandbox Code Playgroud)

excel optimization performance time vba

2
推荐指数
1
解决办法
510
查看次数

标签 统计

excel ×1

optimization ×1

performance ×1

time ×1

vba ×1