+ Reply to Thread
Results 1 to 6 of 6

More efficient alternative to Find-Replace Loop?

  1. #1
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    More efficient alternative to Find-Replace Loop?

    I was tasked with looking through a 10,000 row by 20 column spreadsheet for 628 different ID Codes and replacing them with their Descriptions. The ID Codes could appear individually in any of these cells (200,000 cells!).

    The list of ID's and Descriptions hardly ever changes, so I decided to create the two-dimensional array as part of the find-replace macro shown below (only a few lines of each dimension are shown).

    My question isn't related to that (although if you can suggest a better alternative please do). The main "work" of the macro is the loop at the end of the array declarations, which essentially loops through all 628 ID codes in array dimension 1 and does a Find-ReplaceAll with its corresponding Description from array dimesion 2.

    This task, manually, could take days. My macro has whittled the task down to 2.5 minutes on a 5-year-old laptop, but I was hoping one of the gurus might suggest an even better method than 628 loop iterations. If not, so be it, the end users will appreciate what I've done and then have to find something to do with their "free time."
    Please Login or Register  to view this content.
    Happy Excelling!
    Last edited by Paul; 02-03-2010 at 01:51 AM.

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

    Re: More efficient alternative to Find-Replace Loop?

    No wonderful suggestion here.

    If you had more iterations, I'd suggest doing a dummy Find that sets all of the saved parameters of the Replace, and then pass just What and Replacement arguments in the loop.

    Similarly, two parallel arrays (Find and Replace), instead of one 2D array, might index slightly faster.

    Still, for 628 iterations, I think the difference would be negligable if even measurable.

    This task, manually, could take days. My macro has whittled the task down to 2.5 minutes on a 5-year-old laptop
    Wow, this software stuff might catch on, huh?
    Entia non sunt multiplicanda sine necessitate

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

    Re: More efficient alternative to Find-Replace Loop?

    Hello Paul,

    This method uses the Dictionary Object to make the comparisons. A variant array holds the cell values and is load using a block transfer. On my machine, this takes only a about 1 second to make 200,000 (10,000 rows x 20 columns) replacements.

    I have 3 macros. One to create a list of 628 IDs and descriptions, one to lfill "Sheet1" with random IDs, and the last one to make the comparisons and replace the data. I will include them all here. "Sheet2" is used for the ID and descriptions and "Sheet1" is the test sheet. The total time is calculated at the end of the macro. You see the total time you can use a break point or Debug.Print or copy it to cell.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    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!)

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: More efficient alternative to Find-Replace Loop?

    Thanks, SHG and Leith. I'll play with this later today and see how it goes. Happy Groundhogs Day!

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: More efficient alternative to Find-Replace Loop?

    Leith... amazing. With some adjustments I have the entire macro (creating array, adding it to new sheet, creating DSO, replacing values) running in about 3 seconds.

    I'll definitely have to use DSO more in the future. Thanks a ton!

    Adjusted code shown below. Only thing I noticed is that for cells that weren't replaced, if they were numeric format they were converted to currency format. To get around that I just formatted the entire replacement range as text ahead of the replacement step.
    Please Login or Register  to view this content.

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

    Re: More efficient alternative to Find-Replace Loop?

    Good job, Leith!

+ 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