+ Reply to Thread
Results 1 to 5 of 5

VB - Need to optimise/alternative to copy and paste

Hybrid View

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

    Re: VB - Need to optimise/alternative to copy and paste

    The first thing to look at would be the number of .Select and .Activate commands you have in your code.

    In VBA it's hardly ever necessary to activate or select objects before you use them, so

    ActiveCell.Offset(0, 4).Select
    Set MyRange = Range(ActiveCell, ActiveCell.Offset(0, 2))
    MyRange.Copy
    Range("I3").Activate
    ActiveCell.Offset(Count2, (Count * 3)).Activate
    ActiveCell.PasteSpecial (xlPasteValues)

    Can be cut down to

    Range(Range("I3").Offset(Count2,Count*3),Range("I3").Offset(Count2,Count*3+2)).Value=Range(ActiveCell,.Offset(0,4),ActiveCell.Offset(0,6)).Value
    Last edited by Andrew-R; 10-13-2011 at 03:37 AM.

  2. #2
    Registered User
    Join Date
    10-12-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VB - Need to optimise/alternative to copy and paste

    Ahhh,

    That is a much better way of doing things, I didn't realize how slow the activating and selecting was.

    That decreased it from 20 minutes to 1 minute.

    There was an extra comma in there so the code is :

    Range(Range("I3").Offset(Count2,Count*3),Range("I3").Offset(Count2,Count*3+2)).Value=Range(ActiveCell.Offset(0,4),ActiveCell.Offset(0,6)).Value
    I have to admit, i had to go through that line about 10 times to trace exactly where it was going!!

    I have changed the code in other areas to not use so much select and activate.

    I will attach the files next time, sorry if it was an inconvenience.

    Thanks heaps Andrew for your help, I really appreciate it.

+ 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