简体   繁体   English

循环执行时间太长

[英]Loop is taking too long to execute

I want to first thank you all.我想先谢谢大家。 I have learned a lot from asking question and from you all answering.我从提问和大家的回答中学到了很多东西。 I am starting to get the hang of loops but I am running into an issue where they are taking too long to execute.我开始掌握循环的窍门,但我遇到了一个问题,它们执行时间太长。 My loop below is being asked to perform two different calculations.我下面的循环被要求执行两种不同的计算。 The first one is a percent change and the other is a 4 week CAGR.第一个是百分比变化,另一个是 4 周的 CAGR。 Here is the code:这是代码:

Sub POSCAGR()

    Dim PSpark As Worksheet
    Dim lc As Long
    Dim lr As Long
    Dim qRng As Range
    Dim qCell As Range
    Dim rRng As Range
    Dim rCell As Range
    Dim i As Variant
    Dim j As Variant


'-------------------------------
'Set all variables

    Set PSpark = Worksheets("POS Trend")
    lc = PSpark.Cells(4, Columns.Count).End(xlToLeft).Column
    lr = PSpark.Cells(Rows.Count, "A").End(xlUp).Row
    Set qRng = PSpark.Range("Q4", ("Q" & lr)) 'range for q
    Set rRng = PSpark.Range("R4", ("R" & lr)) 'range for r

'------------------------------
'Calulate WoW changes and 4wk CAGR

        On Error Resume Next
        For Each qCell In qRng.Cells ' this will calculate the week over week changes
            For i = 4 To lr

                PSpark.Cells(i, "Q") = ((PSpark.Cells(i, lc).Value / PSpark.Cells(i, lc).Offset(0, -1).Value) - 1)
                PSpark.Range("Q4", ("Q" & lr)).NumberFormat = "0.0%"
                DoEvents


            Next i
        Next qCell
        On Error GoTo 0

        On Error Resume Next
         For Each rCell In rRng.Cells ' this will calculate a 4 wk CAGR
            For j = 4 To lr

                    PSpark.Cells(j, "R") = ((PSpark.Cells(j, lc).Value / PSpark.Cells(j, lc).Offset(0, -3).Value) ^ (1 / 3)) - 1
                    PSpark.Range("R4", ("R" & lr)).NumberFormat = "0.0%"
                    DoEvents

            Next j
        Next rCell
        On Error GoTo 0



End Sub

This loop has to go through about 600 rows of data and potentially more in the future.这个循环必须遍历大约 600 行数据,将来可能会更多。

Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks,谢谢,

GCC海湾合作委员会

Try this.尝试这个。

Rather than assigning a single seed to a cell, it is faster to put the data into an array and enter it into all cells at once.与将单个种子分配给单元格相比,将数据放入数组并一次将其输入所有单元格会更快。

Sub POSCAGR()

    Dim PSpark As Worksheet
    Dim lc As Long
    Dim lr As Long
    Dim qRng As Range
    Dim qCell As Range
    Dim rRng As Range
    Dim rCell As Range
    Dim i As Variant
    Dim j As Variant

    Dim vDB As Variant, vR As Variant
    Dim n As Long, c As Integer
'-------------------------------
'Set all variables

    Set PSpark = Worksheets("POS Trend")
    lc = PSpark.Cells(4, Columns.Count).End(xlToLeft).Column
    lr = PSpark.Cells(Rows.Count, "A").End(xlUp).Row
    'Set qRng = PSpark.Range("Q4", ("Q" & lr)) 'range for q
    Set qRng = PSpark.Range("Q4", ("r" & lr)) 'range for q & r
    'Set rRng = PSpark.Range("R4", ("R" & lr)) 'range for r
    With PSpark
        vDB = .Range("a4", .Cells(lr, lc))
    End With
    vR = qRng
    n = UBound(vDB, 1)
    c = UBound(vDB, 2)
'------------------------------
'Calulate WoW changes and 4wk CAGR
    For i = 1 To n
        vR(i, 1) = vDB(i, c) / vDB(i, c - 1) - 1 ' column q
        vR(i, 2) = ((vDB(i, c) / vDB(i, c - 3)) ^ (1 / 3)) - 1 'column r
    Next i
    qRng.NumberFormatLocal = "0.0%"
    qRng = vR


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM