+ Reply to Thread
Results 1 to 18 of 18

Speeding up a macro

  1. #1
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Speeding up a macro

    I'm using a worksheet where 2 columns ( E and N ) are used to determine whether to delete rows. Column E will contain values "S", "H" or "D". Column N contains numeric values from 0 to 150000.

    The macro sample works perfectly when several hundred rows are used but really slows down when rows increase to several thousand. Forty thousand rows are commonly encountered and elapsed time is 7 mins.

    Any assistance to speed up this macro would be gratefully appreciated.


    Please Login or Register  to view this content.
    Last edited by Mister P; 12-19-2010 at 03:32 PM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Speeding up a macro

    Hi Mister P

    I should think you could use filtering on this. If you'd post a sample of your file, I'll be glad to look at it.

    Jon
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: Speeding up a macro

    You can use
    Please Login or Register  to view this content.
    rgds

    johnjohns

  4. #4
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Speeding up a macro

    Tried that, JohnJohns. No improvement. Actually the macro ran slower.

    Mister P

  5. #5
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Speeding up a macro

    I'm enclosing a small sample worksheet. The macros in this sample run quick. My full worksheet has over 46,000 rows and really slows down.

    Thanks for looking.

    Mister P
    Attached Files Attached Files
    Last edited by Mister P; 12-19-2010 at 01:35 AM.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Speeding up a macro

    Hi Mr P,

    Try this.
    Please Login or Register  to view this content.
    The idea is to select all the rows first and then only do a single delete. You may need the "EntireRow" and xlUp to make it work correctly.

    hth.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Speeding up a macro

    Hi Mister P

    If you haven't a solution by then, I'll get back to you in the AM. Getting late here.

    John

  8. #8
    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: Speeding up a macro

    I think jaslake's filter suggestion is the best idea.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Speeding up a macro

    Marvin P
    Tried your code in my full worksheet. It works the same as the code I used and didn't gain any time. Both codes ran 4.3 mins duration. In actual use I have to run the macro 3 times which totals to 17 mins.

    I'm wondering if there is a different way to apply the loop.

    Thanks

    Mister P

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Speeding up a macro

    Mister P,

    I believe the looping through all the rows is the real problem. Have you tried Advanced Filters?
    Do a search for "Excel Advanced Filter" or start reading http://www.contextures.com/xladvfilter01.html

  11. #11
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Speeding up a macro

    Marvin P
    I will look into the filter method. I have absolutely no knowledge with this.

    Thanks, Mister P

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

    Re: Speeding up a macro

    Try this
    Please Login or Register  to view this content.
    Hope that helps.

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

    Free DataBaseForm example

  13. #13
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Speeding up a macro

    royUK
    I will try your code later today. Appreciate your help. Will let you know how it works on the large worksheet.

    Finished a test run of your code with very promising results. Is there a way to add an OR agrument so blank cells in col N also trigger a deletion of those rows as well.

    In other words, a row which contains S in col E, <2000 in col N or blank in col N would be deleted.

    Thanks

    Mister P
    Last edited by Mister P; 12-19-2010 at 06:59 AM.

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

    Re: Speeding up a macro

    Try this
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    01-13-2009
    Location
    Prince Rupert, BC
    MS-Off Ver
    Excel 97, 2003
    Posts
    54

    Re: Speeding up a macro

    royUK
    I applied your filter code to my application. I added a feature to my code which changes cells which are blank to a zero value. This allowed your code to provide the exact results I required with an exceptional speed improvement. My loop code was 16.5 mins duration. Your filter code was 2.4 mins duration.

    Appreciate the help

    Mister P

    PS, Tested your modified filter code and it works perfectly without having to add zero's to blank cells.
    Last edited by Mister P; 12-19-2010 at 03:58 PM.

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

    Re: Speeding up a macro

    Glad it worked & it also proved that it's best to avoid loops whenever possible

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Speeding up a macro

    or
    Please Login or Register  to view this content.



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

    Re: Speeding up a macro

    No error handling will result in problems if no cells match the SpecialCells criteria

    There might be more data on a sheet than the actual table, in which case UsedRange is not appropriate.

+ 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