提高循环速度

问题描述 投票:-1回答:1

我正在寻求帮助以提高此Macro的速度。它是一个更大的宏的一部分,但是在单独测试它的每个部分之后,此部分将占用98%的时间。总共,此宏大约需要25-30分钟。工作簿中只有12张纸。每张纸大约有200行50列,这与这段代码无关紧要。我不确定是否有更好的方法可以做到这一点,或者您是否对为什么需要这么长时间有任何见解。

我正在Intel I5-6300U CPU @ 2.4Ghz上运行Excel 2016 64位,最高可加速至3Ghz。运行此marco时,我没有运行任何其他程序时达到2.85Ghz左右。 8GB的RAM。

Sub step3()

Dim sht As Worksheet

Application.ScreenUpdating = False

For Each sht In ActiveWorkbook.Worksheets

    If sht.Name <> "Directions" Then
        'Update months in colA
        sht.Range("A5").Formula = "=EOMONTH(TODAY(),-1)"
        sht.Range("A6").Formula = "=EOMONTH(TODAY(),-2)"
        sht.Range("A7").Formula = "=EOMONTH(TODAY(),-3)"
        sht.Range("A5:A7").Copy
        sht.Range("A5:A7").PasteSpecial xlPasteValues
    End If

Next sht

Application.ScreenUpdating = True
End Sub
excel vba performance loops processing-efficiency
1个回答
4
投票

关闭计算和事件,代码外的某些因素导致了问题。

Sub step3()
    On Error GoTo safeout
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    Dim sht As Worksheet
    For Each sht In ActiveWorkbook.Worksheets

        If sht.Name <> "Directions" Then
            With sht.Range("A5:A7")
                .Formula = "=EOMONTH(TODAY(),-ROW(1:1))"
                .Value = .Value
            End With
        End If

    Next sht
safeout:
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
    Application.EnableEvents = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.