+ Reply to Thread
Results 1 to 5 of 5

Copying values between sheets VERY slow...

  1. #1
    Registered User
    Join Date
    10-20-2009
    Location
    St. Louis
    MS-Off Ver
    Excel 2003
    Posts
    2

    Copying values between sheets VERY slow...

    I'm running a loop that takes the values of certain cells from a range of rows the user has selected on one sheet, and copies them to cells in another sheet in the same workbook.

    It works fine, except it is unbelievably slow... nearly 4 seconds per loop.

    Obviously there is something terribly inefficient about my code, but I can't imagine what it is. Can someone suggest a more efficient way to accomplish this? The slow part is lines 3 and 4 of the 5 lines of code below. Each of those 2 lines are taking nearly 2 seconds to process, each time through.

    The code takes the values from the 4th and 6th columns in the selected rows and places them in Columns A and B, respectively, starting in row 2, of a worksheet named "Cstates", in the same workbook. It needs to work regardless of the number of rows selected, which sometimes may be up to 1000 rows or so, which would take over an hour.

    Please Login or Register  to view this content.
    Help appreciated!
    Last edited by ridetoeat; 04-21-2010 at 06:10 PM. Reason: clarity, added code tag

  2. #2
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Copying values between sheets VERY slow...

    Could you add the example of the data?, ill take a look but want ot see what you mean

  3. #3
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Copying values between sheets VERY slow...

    Hi, unless I've misinterpreted what you require you seem to be simply copying data from one sheet to another sheet, column A=>D and B=>F. You are doing this one row at a time where a copy operation can do this in 2 steps, just like if you selected the cells yourself and pasted across. Is there a particular reason why you have done it cell by cell?

    Besides a lot of unnecessary looping, if you have a large number of formulas in your work book it may be calculating each time, so that's 2,000 times. This can be avoided by turning off calculation prior to copying, perform the copy then turn it back on so it recalculates only once.

    Is that what your after?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Copying values between sheets VERY slow...

    I'd also add to parrynz's point that disabling screenupdating is also advisable when running a cell by cell iteration with values being written to the worksheet (though as outlined already avoiding such iteration is first concern)

    For a very brief overview of "best practice" when it comes to coding see this MS blog: http://blogs.msdn.com/excel/archive/...practices.aspx

  5. #5
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Copying values between sheets VERY slow...

    Yeah,

    Before code

    Applicaiton.Screenupdating = False
    Application.Calculation = xlCalculationManual

    After code

    Application.Screenupdating = True
    Application.Calculation = xlCalculationAutomatic

    Application.Calculate (not sure if this is needed to force one calcualtion)

    For what you have descriped the only thing that i can think of that makes the code run that slow is the fomula.

    When possible its best to avoid using formula & doing the calcuations within VBA, personally i find it runs considerably faster, not to mention the file size is ALOT smaller.

+ 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