+ Reply to Thread
Results 1 to 5 of 5

Slow Do While Loop

  1. #1
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Slow Do While Loop

    Hi,

    I have the code below to look at one sheet, and depending on the results, copy the relevant row to the relevant tab. It takes around a minute to run on my PC (checking 1000 rows), and was wondering if there was any way to optimise it? :-

    Please Login or Register  to view this content.
    I'd be glad of any ideas as I'm sure this can be done more quickly.

    Thanks in advance!
    Last edited by bd528; 09-12-2010 at 08:33 AM.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Slow Do While Loop

    You could turn off screen updating and automatic calculation, you could also stop using paste special to copy the values and instead just define a range on the Temp sheet the same size as the one on the sheet you're copying from and say.

    Please Login or Register  to view this content.
    That would save you having to activate different sheets.

    Things would be lot clearer if you tidied up your case statement. A lot of things there happen whatever the case selected is, so there's no need to repeat that code and embed it inside the select statement.

    Also avoid changing the active cell, you can loop through a range of cells without having to select each one in turn.

    Does that get you started?

  3. #3
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Slow Do While Loop

    Could you sort the sheet, copy in segments and then unsort afterwards? You will need to put in a helper column with sequential numbers in order to sort back to the original order.
    Martin

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

    Re: Slow Do While Loop

    Hello bd528,

    When coding in VBA, it is rare that you need to select or activate an object before you can perform an operation. It is clear that you learned to code from the Macro Recorder. It will take a some time to clean up and optimize the code.
    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!)

  5. #5
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Slow Do While Loop

    Thanks for the suggestions, a lot of good ideas.

    Yes Leith, much of what I have learnt is from the recorder, but I am also learning the recorder's way isnt always the best.

    I've stripped down the code and am now left with this one section which still takes around 30 seconds to run. It starts at the top of the sheet and if it find a 1, its copies the row to another sheet. If it finds a 2, it should do nothing :-

    Please Login or Register  to view this content.
    Any ideas if this can be done faster? Thanks
    Last edited by bd528; 09-12-2010 at 08:32 AM.

+ 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