+ Reply to Thread
Results 1 to 13 of 13

Optimize code copy very slow

  1. #1
    Forum Contributor
    Join Date
    11-08-2017
    Location
    London
    MS-Off Ver
    Office 2007
    Posts
    122

    Optimize code copy very slow

    I have a macro thats been written by the recorder but its exceptionally slow. What can I do speed up the macro. It copies data from range R2-R130 to A23-C400 and call another macro. If I run the code it goes not responding and it takes almost 12 min to run through (:

    Thank you for any pointers and help

    Please Login or Register  to view this content.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Optimize code copy very slow

    The last part could be tidied up to this. You don't normally need to select a cell/range to operate on it but I don't know what the other macros require.

    Oops ... Edit: forgot the code example ...

    Please Login or Register  to view this content.
    Last edited by TMS; 11-19-2017 at 09:41 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Optimize code copy very slow

    Note: you don't need any of the scrolling. And you can combine rows that end with .Select followed by Selection.
    Last edited by TMS; 11-19-2017 at 09:42 AM.

  4. #4
    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: Optimize code copy very slow

    It looks like a macro captured via the recorder.

    The recorder is great for capturing code but it should always be edited and modified to clean it up.

    As Trevor says in general .Select is not necessary. Why not upload the workbook and tell us what you want to achieve. It's almost certain that your code is not the most efficient or elegant way of getting to the end goal.
    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.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Optimize code copy very slow

    I'm guessing you change a cell value, then run a macro to do some calculations, and then store the outcome somewhere.

    Very likely that can be put into a loop.

    We would need to see the rest of the code to determine how to improve the code effectively.

  6. #6
    Forum Contributor
    Join Date
    11-08-2017
    Location
    London
    MS-Off Ver
    Office 2007
    Posts
    122

    Re: Optimize code copy very slow

    Quote Originally Posted by TMS View Post
    The last part could be tidied up to this. You don't normally need to select a cell/range to operate on it but I don't know what the other macros require.

    Oops ... Edit: forgot the code example ...

    Please Login or Register  to view this content.
    Thank you will that copy copy it to the clipbord ? I made changes took out all the scrolls and select but its still terribly slow.Will try your copy and report back thank you

    P>S I have a macro with VBA collections and formulas. If mycopy ranges are 30 the macro is fast but as soon as I am copying 300 lines thats when its slow and not responding.
    Last edited by JohnGreen2; 11-19-2017 at 01:50 PM.

  7. #7
    Forum Contributor
    Join Date
    11-08-2017
    Location
    London
    MS-Off Ver
    Office 2007
    Posts
    122

    Re: Optimize code copy very slow

    Quote Originally Posted by Richard Buttrey View Post
    It looks like a macro captured via the recorder.

    The recorder is great for capturing code but it should always be edited and modified to clean it up.

    As Trevor says in general .Select is not necessary. Why not upload the workbook and tell us what you want to achieve. It's almost certain that your code is not the most efficient or elegant way of getting to the end goal.
    Correct Richard

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Optimize code copy very slow

    Suggest you post a sample workbook with ALL the code and sufficient typical test data to demonstrate the slowness. Ideally, anonymise a real example.

  9. #9
    Registered User
    Join Date
    07-26-2013
    Location
    BL, Bosnia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Optimize code copy very slow

    As stated by couple of people already: recorder is a good first step.. However, recorder records all small things which are usually unnecessary in codes... In your case, possibly loop can be used (if there is any logic behind locations of copy and pastes) + all selection, scrolling, activation can be removed... One thing which can speed up the process a bit is adding Application.ScreenUpdating = False at the beginning of you code and Application.ScreenUpdating = True at the end of your code, but this is not complete solution...

    I agree with TMS, without seeing an example, it is hard to assess how to properly do it...

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Optimize code copy very slow

    Both these versions are untested as the other subroutines are not present. Test on a copy of the workbook!


    "Tidy version"

    Please Login or Register  to view this content.
    "Short version" with loop

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    11-08-2017
    Location
    London
    MS-Off Ver
    Office 2007
    Posts
    122

    Re: Optimize code copy very slow

    Quote Originally Posted by TMS View Post
    Both these versions are untested as the other subroutines are not present. Test on a copy of the workbook!


    "Tidy version"

    Please Login or Register  to view this content.
    "Short version" with loop

    Please Login or Register  to view this content.
    Thank you TMS for the code...........I finally optimized the code that took 12 Seconds and it take 5 seconds now.I have used some of your code thank you for the help TMS. I might add there was a user error involved here -My Macro have read values it should not have read...I manage to find the culprit by braking the macro down in pieces. Thank you TMS and Richard for your help

    JohnGreen2
    Last edited by JohnGreen2; 11-21-2017 at 03:29 AM.

  12. #12
    Forum Contributor
    Join Date
    11-08-2017
    Location
    London
    MS-Off Ver
    Office 2007
    Posts
    122

    Re: Optimize code copy very slow

    Quote Originally Posted by pella88 View Post
    As stated by couple of people already: recorder is a good first step.. However, recorder records all small things which are usually unnecessary in codes... In your case, possibly loop can be used (if there is any logic behind locations of copy and pastes) + all selection, scrolling, activation can be removed... One thing which can speed up the process a bit is adding Application.ScreenUpdating = False at the beginning of you code and Application.ScreenUpdating = True at the end of your code, but this is not complete solution...

    I agree with TMS, without seeing an example, it is hard to assess how to properly do it...
    Thank you I mange to solve it....was more a error as the macro read the wrong code. I moved the procedure and the code works flawlessly now -Thank you

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Optimize code copy very slow

    You're welcome. Thanks for the rep.


    For future reference, please do not quote whole posts; it just clutters the thread. If there is more than one reply, it is useful to quote specific relevant extracts which you are responding to or expanding on.

+ 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. [SOLVED] Current code to cycle through sheets and copy values is very slow - can it be sped up?
    By maym in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-29-2017, 11:28 PM
  2. [SOLVED] Optimize Slow VBA Code, need for speed :)
    By antonio32 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-28-2017, 06:50 AM
  3. Open multiple files and copy data is slow, code after end of loop not working - help?
    By hadamhiram in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2014, 11:52 PM
  4. [SOLVED] Copy and Paste using macro very slow is this code correctly done? Need help with a edit
    By Mr_Bill in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2014, 07:39 PM
  5. Code to insert rows and copy data is too slow
    By muddbog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-13-2014, 10:52 AM
  6. Optimize a slow code...
    By benoitly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2013, 01:44 PM
  7. Copy/Paste code running very slow
    By Zimbo in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-07-2009, 04:40 AM

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