+ Reply to Thread
Results 1 to 15 of 15

Speed up slow macro loop

  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
    3,952

    Speed up slow macro loop

    The macro below does two things: it concatenates the values in Column E, and adds the values in column J, for any duplicates in column B . The sheet it works on has 2246 lines, so the code should operate quickly. However, it takes a little under three minutes to complete (2:58). I'd appreciate some help in speeding it up.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Speed up slow macro loop

    Post the workbook.

  3. #3
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Speed up slow macro loop

    not the fastest option, but you can try
    Please Login or Register  to view this content.

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

    Re: Speed up slow macro loop

    Bob,
    I've attached the workbook.

    Nilem,
    I got an Overflow error at this line:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Speed up slow macro loop

    It looks like you are appending quantities and adding dates, that doesn't sound right.

    Can you give a text description of what you are trying to do, without indicating the columns?

    Also, generically, interacting between VB and the Excel GUI is very slow, I would recommend loading the data into an array, doing your operations there, then pasting the results back to Excel all at once. You could have an array to hold your WPR number, Quantities and Due Dates, calculate and adjust as necessary, paste the Quantity and Due Date arrays back, then delete the extra rows. In your calculate section, you could index sort based on WPR number, which would speed up your searches considerably.

    Reply on my first two questions, I'll see what I can do.

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

    Re: Speed up slow macro loop

    Sure, here you go:

    for any duplicates in WPR_Number I want to A: Concatnenate the line descriptions and B) add the Total costs (sorry, I said J earlier, and meant I), then delete the other duplicate rows. So, in the end, I shouldn't have ANY duplicate WPR_Numbers, but the line description column should have ALL of the line descriptions for that WPR in one cell, and the Total Cost should be the sum of the individual line items for that WPR.

    I'm not that conversant with Arrays, hence my problem today. Any help is greatly appreciated.

  7. #7
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Speed up slow macro loop

    intermediate option
    Please Login or Register  to view this content.
    edited
    should be
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    Last edited by nilem; 05-14-2012 at 11:52 AM.

  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
    3,952

    Re: Speed up slow macro loop

    Nilem,
    I owe you an apology: My original code made use of an extra column which moved all my columns over by 1. I tried your code on my original data, minus that column, which is why I got the overflow on your code. Adjusting from columns 6 and 10 to 5 and 9, your code works flawlessly, in 104 seconds. Still slower than I'd like, but much faster than my original code.

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Speed up slow macro loop

    or
    Please Login or Register  to view this content.

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

    Re: Speed up slow macro loop

    Holy Smokes, Nilem! Your last code just ran in .38 minutes! That's pretty fast. I'm testing it now with my main data and will let you know the results when completed. Thank You!

  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
    3,952

    Re: Speed up slow macro loop

    Nilem,

    Your code checks out. It runs without a hitch, and it's very fast! Would you like to help me work on my car next?

    Thanks so much. I couldn't have done it without you.

  12. #12
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Speed up slow macro loop

    You can try this, it seems to run a bit faster:

    Please Login or Register  to view this content.
    With a QuickSort routine:

    Please Login or Register  to view this content.
    You will want to edit the set command to point to your real worksheet, and I set up some constants to point to the column numbers that you might want to edit as well: colWPR, colDesc, colTotal.

    It essentially does what I was describing earlier: Load the WPR numbers, descriptions and totals into arrays, index sort by WPR number, loop through the sorted WPR's looking for duplicates that haven't been totaled already. For duplicate entries, append to the description and accumulate the totals and set a flag on the record indicating that it has been used as a duplicate. Once all the WPR numbers have been looked at, move the entire description totals array to the spreadsheet. Then loop backwards through the array, building a string of ranges to delete and deleting them. The range object has a limit of 255 characters, so I put a length check and reset in.

    On my machine, it took less than two seconds...
    Last edited by wallyeye; 05-14-2012 at 02:13 PM.

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

    Re: Speed up slow macro loop

    Walleye,

    Sorry to tell you, but Nilem's code is faster. I didn't get a chance to test both yesterday, so did just now. Using the same timer setup, Nilem's code runs in .44 seconds, yours runs in 1.16. Not a big difference between the two; they're both much faster than I had any right to expect. Thanks for showing me a different way to approach the problem.

  14. #14
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Speed up slow macro loop

    No need to be sorry, I'll take a look at his code and try to learn from it.

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

    Re: Speed up slow macro loop

    You and me both.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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