Results 1 to 20 of 68

Thread: Advice needed to speed this code.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    Hi Vishal 120
    I actually tested it this time, I changed the formula so that it made sense on my computer, but the principal is the same. There were a few coding errors ( looping round a negative index doesn't work) this code took 4 secs on my computer see how yours does:
    (I put numbers from 1 to 445 in the rows A1 to A445)
    Note the R1C1 reference in my equation is used to demonstrate changing the formula for each row (or column), I don't know whether your equation needs this but it is a useful technique to know about.


    Sub test()
     startTime = Timer
    With ThisWorkbook.Sheets("Sheet1")
        lLR = Cells(Rows.Count, "A").End(xlUp).Row
    End With
    inarr = Range("AY59:EX" & lLR)
     'assigning the main loading formula
    Application.StatusBar = " Automated Planning : Computing ...."
     '    With Range("AY59:EX" & lLR)
     '        .Formula = "=IF(OR($AF59="""",$AF59="""",$AJ59=""""),0,IF(AY$58<$AJ59,0,IF(AY$58>=$AJ59,IF(0<($AN59),MIN(($AN59-'SUM(AX59:$AX59)),$AO59,SUMIF(Shadow_Dept_Lines_Sum_Col,$AM59,AY$4:AY$56)-SUMIF($AM$58:$AM58,$AM59,AY$58))))))"
     '        '.Application.Calculation = xlCalculationAutomatic
     '        .Value = .Value
     '    End With
    endarray = lLR - 59
    ' column ex is column 154 column ay is column 59
    endcol = 154 - 51
    For i = 1 To endarray
    For j = 1 To endcol
    '    inarr(i, j) = "=IF(OR($AF59="""",$AF59="""",$AJ59=""""),0,IF(AY$58<$AJ59,0,IF(AY$58>=$AJ59,IF(0<($AN59),MIN(($AN59-SUM(AX59:$AX59)),$AO59,SUMIF(Shadow_Dept_Lines_Sum_Col,$AM59,AY$4:AY$56)-SUMIF($AM$58:$AM58,$AM59,AY$58))))))"
        inarr(i, j) = "=sum(r1c1:r" & i & "c1)"
        
        
    Next j
    Next i
    Range("AY59:EX" & lLR).Formula = inarr
     MsgBox Timer - startTime & " secs."
    End Sub
    Last edited by SamT; 05-21-2016 at 08:05 AM. Reason: Added CODE Tags with Editor's # icon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •