+ Reply to Thread
Results 1 to 7 of 7

Speed up a macro which uses instr function

  1. #1
    Registered User
    Join Date
    08-23-2010
    Location
    Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Speed up a macro which uses instr function

    I am trying to speed up this macro. You can see lots of data is sifted through. Basically, the macro will go through a column of data (ce) (quite large) and for each row will find a string match based on another column in another worksheet (NAME2). Once a match is found the value found is inserted a few few rows over from the (ce) at hand This macro works great as is. However, I'm beginning to deal with huge amounts of data and need a much faster process. I have played around with Exit For but get errors related to "no For in Next". I believe much time is wasted sifting through all i values (0 To 30000+) when an early exit might be ideal once a match is found. Thanks. Here is the code below:
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Speed up a macro which uses instr function

    You could use a native Excel formula for this which will generally outperform VBA for speed. Can you post a sample book for me to play with?
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Speed up a macro which uses instr function

    Hi,

    Since you are processing a large amount of cells you may be better advised to write an Excel function in the offset column which will deduce this match. Once you've worked out the function, store it in a named cell at the top of the column out of the way then just get your macro to copy that cell and paste it to all the 31334 cells in one hit. Finally copying and range valuing the column when that's done.

    It's always better to use standard Excel functionality where you can.
    If this isn't practical for you the other option is to read the A column range into a VBA array and process it in VB first before finally writing the updated array back to the worksheet.

    The problem with a loop is that every iteration jumps back to the Excel App and then back to the VBE. It's this jumping backward and forward that carries a large time overhead.

    Regards,
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    08-23-2010
    Location
    Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Speed up a macro which uses instr function

    Here is a sample work book crunched down to just a few data on each sheet. The current speed I can generate the writing is slightly faster than one row per second. I have included a working macro as well.
    Thanks
    Last edited by drcheaud; 08-16-2011 at 10:20 PM.

  5. #5
    Registered User
    Join Date
    08-23-2010
    Location
    Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Speed up a macro which uses instr function

    Quote Originally Posted by tlafferty View Post
    You could use a native Excel formula for this which will generally outperform VBA for speed. Can you post a sample book for me to play with?
    I have posted a sample work book. I hope it helps. Thanks

  6. #6
    Registered User
    Join Date
    08-23-2010
    Location
    Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Speed up a macro which uses instr function

    Quote Originally Posted by drcheaud View Post
    I have posted a sample work book. I hope it helps. Thanks
    Sorry PLEASE see workbook below for more ideal speed conditions. (This has much more data to sift through in NAME2. you'll see what i mean by just slightly faster than 1 row per second when the macro is run from the data page. Thanks again.
    Last edited by drcheaud; 08-18-2011 at 12:17 AM.

  7. #7
    Registered User
    Join Date
    08-23-2010
    Location
    Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Speed up a macro which uses instr function

    Hi Thomas:

    Any luck playing around with this one?

    Thanks,

    DrCheaud

+ 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