+ Reply to Thread
Results 1 to 2 of 2

Optimize for loop in order to win time

Hybrid View

DamienR Optimize for loop in order to... 08-13-2013, 10:41 PM
jason_lee_91 Re: Optimize for loop in... 08-13-2013, 11:39 PM
  1. #1
    Registered User
    Join Date
    07-16-2013
    Location
    Ho Chi minh
    MS-Off Ver
    Excel 2007
    Posts
    13

    Arrow Optimize for loop in order to win time

    Dear all,

    i am working on a huge excel document (10000 rows more than 80 columns)
    I code a macro to reduce the size of the document, and it is working, the problem is that my macro is really slow to update all the datas, and i don't know (yet) how to optimize my code to win some time.

    The document is quiet sensitiveBackgroung For loop optimizing.xlsx so i will just give you my code and a sample of the doc.
    Sub Sumiftest()
    
    Dim j As Integer
    Dim i As Integer
    
    
    'define the range where you want to update the datas i = depart row To end row
    ' S,B and OPO are different sheet of the workbook
    
    For i = 1 To 10
    
    Cells(i, 8).Select
    
    'first 8 columns are define by a formula in each cells so here i offset after each cell
    Cells(i, 8).Value = WorksheetFunction.SumIf(Range("'S'!H:H"), Cells(i, 5).Value, Range("'S'!L:L"))
    ActiveCell.Offset(0, 1).Select
    
    ActiveCell.Value = WorksheetFunction.SumIf(Range("'S'!H:H"), Cells(i, 5).Value, Range("'S'!K:K"))
    ActiveCell.Offset(0, 1).Select
    
    ActiveCell.Value = WorksheetFunction.SumIfs(Range("'B'!G:G"), Range("'B'!C:C"), Cells(i, 5))
    ActiveCell.Offset(0, 1).Select
    
    ActiveCell.Value = WorksheetFunction.SumIf(Range("'OPO'!D:D"), Cells(i, 5).Value, Range("'OPO'!N:N"))
    ActiveCell.Offset(0, 1).Select
    
    ActiveCell.Value = WorksheetFunction.SumIf(Range("'S'!H:H"), Cells(i, 5).Value, Range("'S'!G:G"))
    ActiveCell.Offset(0, 1).Select
    
    ActiveCell.Value = Range("H" & i) + Range("K" & i) - Range("I" & i) - Range("J" & i)
    ActiveCell.Offset(0, 1).Select
    
    ' here, i have a problem with WorksheetFunction.Max( where i have a 1004 error, "unable the get max function of etc.."
    ActiveCell.Value = Range("H" & i) + Range("K" & i) - Range("I" & i) - WorksheetFunction.Average(WorksheetFunction.Max(Range("AT" & i) + Range("AU" & i), Range("AV" & i)), WorksheetFunction.Max(Range("AW" & i) + Range("AX" & i), Range("AY" & i))) / 26 * Cells(i, 12)
    ActiveCell.Offset(0, 1).Select
    
    ActiveCell.Value = Range("H" & i) - Range("J" & i)
    ActiveCell.Offset(0, 1).Select
    
    ' the 78 other columns are define by the same formula so here i made a loop on column
    For j = 1 To 78
    ActiveCell.Value = WorksheetFunction.SumIf(Range("'B'!C:C"), Cells(i, 5).Value, Range("'B'!G1").Offset(0, j).EntireColumn)
    ActiveCell.Offset(0, 1).Select
    Next j
    
    ' those line are here because in the file one row upon two are used. And sometimes two rows are blanked.
    If IsEmpty(Cells(i, 5)) = False Then
    i = i + 1
    End If
    
    If IsEmpty(Cells(i, 5)) = False Then
    i = i
    End If
    
    'finish to work on the line
    Next i
    
    End Sub
    The code is working ( when the 1004 error doesn t want to show up) - please believe me.
    If someone as any suggestion to win time, please don't hesitate to answer.

    Thanks

  2. #2
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: Optimize for loop in order to win time

    Use relative references to make all your formulas in one go.

    You have a whole bunch of something like
    Range("A1").select
    Activecell.value = "=A2+A3"
    Activecell.offset(0,1).select
    Activecell.value = "B2+B3"
    Instead you should do something like

    Range("A1:B1").formulaR1C1 = "R2C+R3C"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop thru files in chronological order
    By cokillerliu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-02-2013, 10:02 AM
  2. Order of controls in For Each Loop
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-23-2005, 02:05 PM
  3. Nested loop order?
    By jclark419 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2005, 03:05 PM
  4. Optimize copy/paste loop
    By aldsv in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-09-2005, 06:07 AM
  5. [SOLVED] Optimize VBA Excel 2003 NextFor loop
    By eric_rutt@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2005, 09:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1