Suggestions on how to speed up the cycle

I have the following code. I was wondering if there is an easy way to rewrite it so that it takes less time to start? I currently have about 13,000 lines to go through, and it will take about 3-5 minutes. Thank you

Sheets("wkly").Activate

Dim i As Long

Lastrow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To Lastrow


If Range("S" & i) > 0.005 Then
        Range("Z" & i, "AA" & i).Copy
        Range("AC" & i, "AD" & i).PasteSpecial xlPasteValues
End If

Application.ScreenUpdating = False
Next i
+1
source share
3 answers

Given all the good tips and include the following. Please try and see how much you can improve performance.

Application.Calculation = xlCalculationManual

lastrow = Range("S" & Rows.Count).End(xlUp).Rows
For i = 1 To lastrow
    If Range("S1").Offset(i) > 0.005 Then
            Range("AC").Offset(i).Resize(1, 2).Value = Range("Z").Offset(i).Resize(1, 2).Value
    End If
Next i
0
source

I believe that this will help to do it much faster. No loops and no need to copy and paste.

Application.ScreenUpdating = False
Application.Calculation = xlManual

Dim wks As Worksheet, Lastrow As Long
Set wks = Sheets("wkly")

With wks

    Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

    .Range("S1:S" & Lastrow).AutoFilter 1, ">.005"

    'Assumes you will always have values greater than .005, if not you need to error trap
     Dim rngFilter As Range
     Set rngFilter = .Range("S2:S" & Lastrow).SpecialCells(xlCellTypeVisible) 'assumes row 1 is header row

     rngFilter.Offset(, 10).Value = rngFilter.Offset(, 7).Value
     rngFilter.Offset(, 11).Value = rngFilter.Offset(, 8).Value


End With

Application.ScreenUpdating = True

UPDATE , , , , , :

Dim wks As Worksheet, varStore As Variant, Lastrow As Long, i As Long

Application.ScreenUpdating = False
Application.Calculation = xlManual

Set wks = Sheets("wkly")

With wks

    Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

    varStore = .Range("S2:S" & Lastrow)

    For i = LBound(varStore, 1) To UBound(varStore, 1)

        If varStore(i, 1) > 0.005 Then .Range("AC" & i + 2 & ":AD" & i + 2).Value = .Range("Z" & i + 2 & ":AA" & i + 2).Value

    Next

End With

Application.ScreenUpdating = False
+2

, , , , . 0.04s ( , ):

Dim wks As Worksheet
Dim varCompare As Variant, varSource As Variant, varTarget As Variant
Dim Lastrow As Long, i As Long

Application.ScreenUpdating = False
Application.Calculation = xlManual

Set wks = Sheets("wkly")

With wks

    Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

    varCompare = .Range("S2:S" & Lastrow)
    varSource = .Range("Z2:AD" & Lastrow)
    varTarget = .Range ("AC2: AD" & Lastrow)
    For i = LBound (varCompare, 1) To UBound (varCompare, 1)

        If varCompare (i, 1)> 0.005 Then
            varTarget (i, 1) = varSource (i, 1)
            varTarget (i, 2) = varSource (i, 2)
        End if
    Next

    .Range ("AC2: AD" & Lastrow) .Value = varTarget
End with

Application.ScreenUpdating = False
+1
source

Source: https://habr.com/ru/post/1615515/


All Articles