+ Reply to Thread
Results 1 to 15 of 15

Rewrite code to use less memory

  1. #1
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Rewrite code to use less memory

    I posted this problem a few days ago, here.
    Now that I understand the problem a little better, I’m posting it again (hopefully a little clearer)

    I have a macro that performes a simple task. It matches rows from sheet2 with rows in sheet1 if their respective value in column A match.
    However, when I have a lot of data, I get a memory error and can't run the code.

    This is an example of a smal dataset (the line that causes the error):
    Please Login or Register  to view this content.
    My question: Is there a way to break this up into several separate operations?
    E.g. first find matches for row 1 and 2, then row 3 and 4, etc.

    I’m stuck, so I appreciate all feedback!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Rewrite code to use less memory

    That code extract works for me and generates
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    in cell I1, copied across and down to cell M10.

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    Hi Karl,

    Try this:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Rewrite code to use less memory

    Wow!
    That looks great, tested it now and I think it's exactly what I'm looking for (and much faster than what I had)
    Thank you very much, Xladept

    My sheets may have more rows and columns, is there any reason why this is not a good way to generalize it?

    Please Login or Register  to view this content.
    Last edited by Karl Gustaf Karsten; 02-23-2016 at 03:50 PM.

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    Hi Karl,

    I kept the 1's with thr 1's and the 2's with the 2's and got rid of that Column letter:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Rewrite code to use less memory

    Perfect! This is beautiful

    Thanks again, this is very very helpful!

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    You're welcome and thanks for the rep!

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    Hi Karl,

    Here's a quicker version for your big file:

    Please Login or Register  to view this content.
    Last edited by xladept; 02-23-2016 at 09:18 PM.

  9. #9
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Rewrite code to use less memory

    Hi, thanks again!

    I'm sure it's faster but I get a memory error on this line (on a laptop)
    Please Login or Register  to view this content.
    However, you've already reduced my run time from hours to seconds - And I'm very exited about that

    I'll try it again tomorrow on a desktop.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    Hi Karl,

    Thanks for the rep!

    How big are your worksheets?

    Let me know - I wonder if the Array size is limited?

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    Hi Karl,

    If you need to rerun it, you'll want this code (or the quick code):

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Rewrite code to use less memory

    Hi again,

    Sheet1 has 140 000 rows and goes to column DA
    Sheet2 has 250 000 rows and goes to column AK

    I might need to work with bigger sheets in the future, but not significantly bigger, so I think this will do it

    I ran the quick code again and got the out of memory error, here:
    Please Login or Register  to view this content.

    I timed Sub KarlK() and Sub KarlKLG() and both used 22 seconds, is the latter better?

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    The only difference in the two routines is that you can run the second one over and over - the first one would keep growing the LC1!

    * I wonder if your configuration is adequate? Do you have your core partitioned or are you short on RAM? I've processed larger books than that - I have 4GB of RAM and 367GB of free space.

    ** but 22 seconds? that code certainly fulfills the requirement
    Last edited by xladept; 02-25-2016 at 01:40 PM.

  14. #14
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Rewrite code to use less memory

    Ok, thats good to know.

    I'm not sure about the configuration at all - My (work) computer has access to my partition (40GB free) through a network, and I do not have access to the C: drive. System information says 8GB of RAM though.

    Anyway, thanks again for taking the time to help!
    It makes this project a lot easier for me so I really appreciate it

  15. #15
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Rewrite code to use less memory

    Well - the "slow" program satisfies the "use less memory" criterion - glad it worked out

+ 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. rewrite advaced filter code in shorter simpler vba
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-26-2016, 01:47 PM
  2. To use the code rather than rewrite
    By gan_xl in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2015, 04:53 AM
  3. How do I rewrite this code to maintain the cell format?
    By Bran99 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-10-2015, 04:00 PM
  4. rewrite the existing code for charts
    By csunilkumar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-14-2014, 12:49 AM
  5. [SOLVED] Is there a way to rewrite this code for a UserForm in loop form?
    By bananajelly in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2014, 01:21 PM
  6. [SOLVED] Looking for help with speeding up or rewrite on vba code.
    By ukchris74 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-16-2013, 01:54 PM
  7. [SOLVED] Outlook 2010: Rewrite VBA Code
    By Cansa in forum Outlook Formatting & Functions
    Replies: 3
    Last Post: 10-12-2012, 12:25 PM

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