+ Reply to Thread
Results 1 to 18 of 18

Speed up a macro

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Speed up a macro

    I've got a rather involved macro that's running kind of slowly, and I would appreciate any help I can get speeding it up. It's in two parts; the first is to create and email a report, the second is to format so it's pretty for printing. The full codes for both routines is pasted below.

    The email part I developed first and it runs pretty quickly. Afterwards, I added the second macro, which is called halfway through the first.

    Stepping through the code in the second macro, the problem I see is in this section, the setup for setting the heighth of merged cells in the report:
    Please Login or Register  to view this content.
    Stepping through it, it seems to repeat hundreds of times, and seems to really bog down the response time.

    Here are the two macros. The problem (what I see as the problem) is presented first, the overall email macro presented second. I appreciate any help you can provide on this.

    Please Login or Register  to view this content.
    Here's the Mail routine:
    Please Login or Register  to view this content.
    Last edited by Andy Pope; 02-21-2010 at 08:08 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Speed up a macro

    Loops will always slow down code, formatting code tends too beslow.

    It's good practice to avoid merged cells
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    I'm very aware that merged cells are a no-no, but sometimes there is no choice.

    In debugging code, if I know a loop is going to slow it down but I also want to check the rest of the code, is there a way to have Excel run the loop only, so I don't have to press F8 a million times?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Speed up a macro

    Hello Jomili,

    The loop is slow because it is testing each cell in the UsedRange. If the Merged cells only occur in specific columns, the code could be sped up by checking only those columns.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    Leath,

    Thanks for chiming in. The macro looks at rows, not columns (I HOPE!).

    The overall process is like this:
    Run test, paste results into sheet (unknown number of lines)
    Past the solution
    Skip a line
    Run the next test, paste results into sheet (unknown number of lines)
    Past the solution
    Etcetera

    So I don't know how many lines the finished product should be, nor how many or which lines will contained the merged data.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Speed up a macro

    Hello Jomili,

    If you could post the workbook for review, we could provide you with a better answer.

  7. #7
    Forum Contributor
    Join Date
    08-09-2005
    MS-Off Ver
    2003 & 2007
    Posts
    111

    Re: Speed up a macro

    Quote Originally Posted by jomili View Post
    In debugging code, if I know a loop is going to slow it down but I also want to check the rest of the code, is there a way to have Excel run the loop only, so I don't have to press F8 a million times?
    You can put a stop-point after the loop. Click in the left margin next to the first statement after your loop; a brown bullet appears and the statement is highlighted. Then press F5; your entire loop will run. Then continue pressing F8 to step through your code.

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    dlh,
    Thanks for the tip. That's a good one to know.

    Leath,
    The last time I was asked to, and did, post my workbook, I recieved a very rude note telling me that no one was going to look through all of that code. That's why I only posted the pertinent sections before.

    However, since you requested, please let me FIRST say I do NOT expect you to check all of my code, as this thing does a lot, and it's only in the Mailing section that I'm having trouble. Right now the Checklist is solving 5 steps in a 10 step checklist, so it will grow larger, and I want to make sure it's running as fast as it can.

    With that said, here are 3 workbooks. Open ESS or APR first (both need to be open), then open the Checklist and follow the instructions. Both ESS and APR have been severely stripped down; the APR normally has about 20,000 lines of data, so it's tiny now, thought it's still a large workbook. Feel free to ask me any questions.

    Thanks a lot!

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    I'm still in need of help on this one.

  10. #10
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Speed up a macro

    hi Jomili,

    DLh's suggestion of pressing in the left margin creates a breakpoint. These only exist for the time the file is open. An alternative is to type the word "Stop" on a line by itself immediately after the loop & this will always be there (if you save the file) until you delete it.

    Have you attached your workbooks?
    (they aren't showing up on my screen)

    To add to the comments about limiting the range searched, I recommend moving the "Redim" out of the loop because this can slow the macro down. It may not help at all but you won't know until you try. To do this "do a dry run of the loop" to identify how large the array needs to be, Redim ONCE using the correct size, then process your loop again with actions.

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  11. #11
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    Sorry, apparently my mind is going; I THOUGHT I'd posted my workbooks. Here they are, and there are 3 of them in the Zip file. Open APR and ESS first, then Checklist. Checklist will run through the whole procedure and you'll be able to see where the slowdown is.

    While I understand a little of what you're saying about redim, I'm not sure how to carry it out, so would appreciate a bit more help on that.

    Thanks!
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Speed up a macro

    A shortcut to quickly run over loops and other subroutine is the hotkey Shift+F8.
    If you accidentally stepped into a loop or call a different subroutine and you wish to get out, press Ctrl+Shift+F8 to skip over the loop or jump back into the original procedure.

    The loop will still be run, but it won't break at any point within the loop.

    Another good hotkey is Ctrl+F8. Where you can place the cursor at the line of interest, press it, and the code will run and break at the line where the cursor is.

  13. #13
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Speed up a macro

    hi,

    I'm not sure if my way of moving the redim out of the loop will help or not (esp as I have just done it by duplicating the loop!*), but I have made a few other changes including the removal of a .select which will have been slowing the code down. Note, I have not tried testing my changes, I've just made changes as I read through from top to bottom.

    *If you find a way to very quickly count merged cells, that would be a better method.

    hth
    Rob
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    Thanks for the attempt at cleaning up my code. I went through all your changes, and corrected and changed a few things in mine, but the new routine you put in for the Merged Cells is still not working. Here's that particular code as it now stands:
    Please Login or Register  to view this content.
    You had noted out the line
    Please Login or Register  to view this content.
    but I had to put it back in to get it to run. Now it's giving a run-time error code 1004 Application-Defined or Object-Defined error at this line:
    Please Login or Register  to view this content.

  15. #15
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Speed up a macro

    hi,

    Sorry about that, my previous attempt was shocking - I should have tested it.

    The reason the With statement was failing was due to the string array not being populated because I hadn't reset the Set ranges (after the first loop), therefore the tests were all failing. Also, I found out from the Help files that the mergearea needs a single cell reference.

    I've had another go & have tested it this time :-)
    btw, when I run through it I'm getting an error as the PT is created but if I press Debug & then [F5] the code finishes running as normal.

    To help speed this code up even more (assuming my changes have done anything?), I would try testing for the number of columns in a merged section, skipping over them & then processing the next non-merged cell (perhaps a Do ... Until loop for each column wrapped inside a Do... Until loop for each row).

    hth
    Rob
    Attached Files Attached Files

  16. #16
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    Rob,

    They've locked us down here at work, so I can't see your zipped file (I couldn't even if it was unzipped). Could you just post the pertinent code changes?

    Thanks for all your help.

  17. #17
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Speed up a macro

    hi Jomili,

    I made a few more changes in other sections of the code too (in case you do get a chance to download it) but here's the Format macro...

    Please Login or Register  to view this content.
    I removed the preceding loop (see code comments) - hopefully the impact of creating an array of this size is okay while the elements are empty. Once the the loop has completed the array is resized.

    I made one other change in the NewTry macro because I show file extensions on my computer:
    Please Login or Register  to view this content.
    hth
    Rob
    Last edited by broro183; 01-22-2010 at 05:01 PM. Reason: fixed my code tags

  18. #18
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Speed up a macro

    Rob,
    I apologize it's taken so long to reply. The code changes you made really did the trick. It went from a 45-second timeframe to about 10 seconds. Thank you so much!

    You asked a few questions in your code that I couldn't answer, as I did not create the original code, so am having a tough time following everything that's happening. You said:
    Please Login or Register  to view this content.
    but I'm not sure what test you're referring to. You also asked:
    Please Login or Register  to view this content.
    but I don't know if I'm using the loop or not, so am uncertain how to proceed.

    Again, thanks so much for your help!

+ 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