+ Reply to Thread
Results 1 to 9 of 9

VERY SLOW Do While Loop Macro

  1. #1
    Registered User
    Join Date
    04-04-2015
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    88

    VERY SLOW Do While Loop Macro

    Hi,
    I have a macro that looks like this:

    Please Login or Register  to view this content.
    It takes about a minute to finish the macro loop.
    It is going through a table of 270,725 cells.
    Is there any way to speed this up?

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: VERY SLOW Do While Loop Macro

    In general manner to go faster avoid direct access to cells.
    Copy the complete used range to an array, then treat the data inside the array, then copy back the modified array to the range.
    If you could attach a short sample of your data, it will possible to prepare an update
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    03-26-2014
    Location
    Nowhere, somewhere
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: VERY SLOW Do While Loop Macro

    In general, I agree with PCI concerning using an array. However, you may get adequate performance by just restructuring your variable definitions a bit.

    You are defining most of the variables a Ranges.
    Please Login or Register  to view this content.
    However all you appear to be interested in is their respective text value. These ranges all are from row 2 and are not modified by the code, hence their values remain the same during execution. To prevent repeated access to these Ranges in the "Select Case" statements, these variable can be redefined as String.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-04-2015
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    88

    Re: VERY SLOW Do While Loop Macro

    TnTinMN,
    Thanks for your help. I tried it out but the macro is now 3x slower. Original took 1.5min, yours took 4.5min.

    PCI,
    I tried to make it in an array, and I've done that part successfully. However, I'm having trouble looping through these array.
    Please Login or Register  to view this content.
    I've tried to run it but it ran over 10+ min when I cancelled it.
    When I cancelled it, it gave me an error "Run-Time error '9': Subscript out of range" and highlighted line "Select Case arrClass(r, c)"
    I added "*" comment on the highlighted line.

    Also, I added "**" comment on lines ".Cells(r, c + 1) = vbNullString" lines where I don't know if that is the reason why it is taking so long.
    Is there a way to make arrClass(r, c + 1) = vbNullString and when loop finishes, it updates the table to equal the array?

    Thanks guys

  5. #5
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: VERY SLOW Do While Loop Macro

    PHP Code: 
    Redim Arr(1 to 11 to 10) As String
     Dim J
    %
     
     For 
    2 To 20 Step 2
        Arr
    (1J/2)= Cells(2J).Value 
     Next j 

  6. #6
    Registered User
    Join Date
    04-04-2015
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    88

    Re: VERY SLOW Do While Loop Macro

    Sa DQ,
    Sorry, I don't understand that code... How does it apply here?

  7. #7
    Registered User
    Join Date
    04-04-2015
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    88

    Re: VERY SLOW Do While Loop Macro

    I figured it out how to array and put back to sheet. And now it takes 7seconds (from 1m30sec).

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-26-2014
    Location
    Nowhere, somewhere
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: VERY SLOW Do While Loop Macro

    Quote Originally Posted by cocacrave View Post
    TnTinMN,
    Thanks for your help. I tried it out but the macro is now 3x slower. Original took 1.5min, yours took 4.5min.
    Oops. I used the Text property on the Range and that is slow as it needs to format the value.

    This is probably not needed, but here is the revised code in case you want to test it against the array method. I also change the Mid function to use MID$ as that is supposedly faster.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: VERY SLOW Do While Loop Macro

    "I figured it out how to array and put back to sheet. And now it takes 7seconds (from 1m30sec)."
    Good news
    You could also see ( NOT TESTED here )
    change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    and use
    Please Login or Register  to view this content.
    and after see with

    Please Login or Register  to view this content.
    In the way to did you save memory but perhaps a bit slower
    Last edited by PCI; 05-17-2015 at 04:29 AM.

+ 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. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  2. [SOLVED] slow computer (slow clipboard) breaks my macro
    By twilsonco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2013, 09:16 PM
  3. [SOLVED] Speed up slow macro loop
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-15-2012, 11:41 AM
  4. Slow Do While Loop
    By bd528 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-10-2010, 02:22 PM
  5. How do I slow down my loop ?
    By svanni in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2006, 06:48 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