+ Reply to Thread
Results 1 to 18 of 18

Excel 2007 : Macro - loop slows down

Hybrid View

IALTO Macro - loop slows down 05-19-2010, 04:29 PM
NBVC Re: I need help with my... 05-19-2010, 04:40 PM
shg Re: I need help with my... 05-19-2010, 04:43 PM
IALTO Re: I need help with my... 05-19-2010, 04:55 PM
NBVC Re: I need help with my... 05-19-2010, 04:57 PM
shg Re: I need help with my... 05-19-2010, 05:08 PM
IALTO Re: Macro - loop slows down 05-19-2010, 05:20 PM
shg Re: Macro - loop slows down 05-19-2010, 05:36 PM
DaveF Re: Macro - loop slows down 05-20-2010, 12:41 PM
shg Re: Macro - loop slows down 05-20-2010, 12:50 PM
IALTO Re: Macro - loop slows down 05-20-2010, 12:50 PM
  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

    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

  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, ...
    Option Explicit
    
    Sub SG()
        Dim rExp        As Range
    '    Dim rCom        As Range
    '    Dim rPrm        As Range
        Dim rInp        As Range
        Dim rERC        As Range
        Dim rERP        As Range
    
        Dim iRow        As Long
        Dim nRow        As Long
    
        With ThisWorkbook
            Set rExp = .Worksheets("MGN").Range("ER")
    '        Set rCom = .Worksheets("MGN").Range("ER")
    '        Set rPrm = .Worksheets("MGN").Range("ER")
            Set rInp = .Worksheets("Input").Range("MacroInput")
            Set rERC = .Worksheets("MGN Detail").Range("ERCopyRange")
            Set rERP = .Worksheets("MGN Detail").Range("ERPasteTarget")
        End With
    
        nRow = rExp.Rows.Count
    
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            rInp.ClearContents
    
            For iRow = 1 To rExp.Rows.Count
                If iRow Mod 100 = 0 Then Application.StatusBar = "Row " & iRow & " of " & nRow
                rInp.Value = rExp(iRow, 1).Value
                ' calculate something here if necessary
                rERP.Offset(iRow - 1).Value = rERC.Value
            Next iRow
    
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
        End With
    End Sub
    Last edited by shg; 05-20-2010 at 12:34 PM. Reason: 'calculate' in wrong place

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

    Re: Macro - loop slows down

    In this code:


    Sub Macro1()
            Range("Target").Value = Range("Source").Value
    End Sub
    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.

  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

    Sure, that's a good guess. If so,
    Range("Target").Value = Range("Source").Value2

  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

    daveF,
    This is the same complication I am experiencing.

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

+ 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