+ Reply to Thread
Results 1 to 18 of 18

Excel 2007 : Macro - loop slows down

  1. #1
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Macro - loop slows down

    I created a macro that runs an iterative process. The macro basically makes some adjustments to some data and copies the data to a blank row. Then, the macro does the same thing over and over.

    There are 13000 rows the macro cycles through and I noticed around row 4000, the macro's speed essentially comes to a stop. I also noticed at row 4000 my computer usage (via the task manager) jumps from 60% to 90%.

    I assume it is not a coding issue since the macro runs for awhile, but then hits a wall in the middle of the iterative portion.

    Does anyone have ideas as to what is going on?
    Last edited by IALTO; 05-21-2010 at 10:25 AM.

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: I need help with my lethargic macro

    Welcome to the forum,

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: I need help with my lethargic macro

    And when you do that, post the code. No one can give you advice on a code problem sight-unseen.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: I need help with my lethargic macro

    Well I guess i could be. I was thinking it wasn't due to the fact that the code runs nearly half way and then slows down.

    Here is portion of the code I am struggling with:

    Sub SG()

    Dim Exp As Excel.Range, Comm As Excel.Range, Premium As Excel.Range
    Dim i As Integer

    Set Exp = ThisWorkbook.Sheets("MGN").Range("ER")
    Set Comm = ThisWorkbook.Sheets("MGN").Range("ER")
    Set Premium = ThisWorkbook.Sheets("MGN").Range("ER")

    Application.ScreenUpdating = False

    ThisWorkbook.Sheets("Input").Range("MacroInput").Value = ""
    ThisWorkbook.Sheets("MGN").Range("ER").ClearContents


    For i = 1 To Exp.Rows.Count

    Application.StatusBar = "Calculating the experience rate for group number " & i & " of " & Exp.Rows.Count

    ThisWorkbook.Sheets("Input").Range("MacroInput").Value = ExpRate.Cells(i, 1).Value

    ThisWorkbook.Sheets("MGN Detail").Range("ERPasteTarget").Offset(i - 1).Value = ThisWorkbook.Sheets("MGN Detail").Range("ERCopyRange").Value

    Next i

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: I need help with my lethargic macro

    shg, I'll let you have the honors....

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: I need help with my lethargic macro

    IALTO,

    We're happy to have you here, and more than willing to help -- provided you first take a few minutes to read ALL the forum rules, and then bring this thread into compliance. For now, that means changing the thread title and editing your post to add CODE tags.

  7. #7
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro - loop slows down

    Please Login or Register  to view this content.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro - loop slows down

    ExpRate is undefined.

    As a row variable, i should be declared as a Long, not an Integer.

    You're writing to the statusbar in every loop iteration. You could do it every 10th or 100th iteration.

    If your changes are causing portions of the workbook to recalculate, you should turn calculation off and calculate only whatever portion you need to within the loop.

    You make repeated and unnecessary references to ThisWorkbook, which could be eliminated with a With statement or setting some new object variables.

    You've set the three range variables to the same range -- why?

    So there's a lot you could do, but I can't tell what the whole procedure is doing, so it's hard to be specific.

    How big is range ER?
    Last edited by shg; 05-19-2010 at 05:38 PM.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro - loop slows down

    Without seeing the workbook, or knowing if a loop is even necessary, ...
    Please Login or Register  to view this content.
    Last edited by shg; 05-20-2010 at 12:34 PM. Reason: 'calculate' in wrong place

  10. #10
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro - loop slows down

    Please Login or Register  to view this content.
    Thank you very much for your suggestions. Is there a way to turn calculations on for a specific range rather then the entire workbook? I'm referring to the third line in the above code.

    Possibly something like:
    Please Login or Register  to view this content.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro - loop slows down

    Possibly something like:
    Please Login or Register  to view this content.
    That exact syntax will work. It doesn't turn calculation back on for that range, but it does calculate it once with all dependencies. However, what you'd actually do, for the reasons previously described, is
    Please Login or Register  to view this content.
    See Help for the Calculate method.

  12. #12
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro - loop slows down

    Everything seems to run extremely well now. The only issue I am having is that the calculation in the code truncates everything to 2 decimals. Any ideas?

    Thanks.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro - loop slows down

    The code doesn't truncate anything. Change the formatting to General.

  14. #14
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro - loop slows down

    So the formatting of the cells in the workbook matter?

    I am displaying cells in the workbook with only two decimals, but they have much higher precision. The format of the cells the macro is copying are currencies.

    When the macro finishes, all the copied data is truncated to two decimals.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro - loop slows down

    Having no idea what's in your workbook, I could only speculate. Maybe you have Precision as displayed ticked.

  16. #16
    Registered User
    Join Date
    02-27-2007
    Posts
    38

    Re: Macro - loop slows down

    In this code:


    Please Login or Register  to view this content.
    If the "Source" range is formatted as currency, then the copying will truncate the values to 2 decimal places (without regard to the format of "target". If "Source" is formatted as numbers or general, it will maintain the full precision.

  17. #17
    Registered User
    Join Date
    05-19-2010
    Location
    San Diego, California
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro - loop slows down

    daveF,
    This is the same complication I am experiencing.

    shg,
    'Precision as displayed' was not the culprit.

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro - loop slows down

    Sure, that's a good guess. If so,
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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