+ Reply to Thread
Results 1 to 9 of 9

Copy formulae to range without Copy/Paste

  1. #1
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Copy formulae to range without Copy/Paste

    I want to copy the formulae in cells A1:D1 to a range A10:D20 without using copy/paste.
    The result will be to repeat the formulae in the first 4 cells of row 1 into the first 4 cells of rows 10 to 20 in a relative manner.
    I am aware of the concept for copying values without copy/paste, but substituting .formula for .value does not work.
    It may be simple, but so far it has eluded me!

    Thanks
    A

  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,457

    Re: Copy formulae to range without Copy/Paste

    In what way does it not work? What are the formulae you want to copy? What does your code do?

    Suggest you post a sample workbook.

    Regards, TMS
    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 Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Re: Copy formulae to range without Copy/Paste

    Thanks TMS,

    Maybe I have not explained myself well.
    This is a hypothetical situation.
    In exploring the VBA functions it is clear that using the clipboard to copy and paste is very inefficient.

    eg:

    if the formula in A1 = 1+A2 , B1 = 2+B2, C1 = 3+C2, D1 = 4+D2,

    Range("A1:D1").Copy
    Range(Cells(10, 1), Cells(20, 4)).Select
    ActiveSheet.Paste

    will copy all cell attributes of the first range into the second range, and the results are correct,but takes 2 seconds to run.

    Using the range method,

    Range(Cells(10, 1), Cells(20, 4)).Value = Range("A1:D1").Value

    will instantly duplicate the values of A1 to D1 into the larger range and, as expected, all the values in a column are the same.

    Trying to use a similar function for replicating the formula (relative, just as in the copy/paste method)
    eg

    Range(Cells(10, 1), Cells(20, 4)).Formula = Range("A1:D1").Formula

    does NOT give a relative result: the formula in A10 becomes 1+A2 whereas it should be 1+A10
    (FormulaR1C1 and FormulaLocal are no better)

    So I am looking for a Formula method similar to the Value method.

    Suggestions appreciated!

    A

  4. #4
    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,457

    Re: Copy formulae to range without Copy/Paste

    If that takes 2 seconds, there's something wrong with your setup

    However, this:

    Please Login or Register  to view this content.
    could be this:
    Please Login or Register  to view this content.
    Or, you could do this:
    Please Login or Register  to view this content.
    Do you have a Change event handler active on that worksheet? Because, if you have, that could be looping ( a lot ).


    Regards, TMS

  5. #5
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Re: Copy formulae to range without Copy/Paste

    Thanks TMS,

    I am running these tests on a blank worksheet, so there should not be any spurious effect going on affecting run time.

    Your Test2 gives the right values result and takes 1 second whereas Test1 (copy/Paste) takes 2 seconds.
    So that's a good improvement.

    Your Test3 gives an instant result with the right values (but does not reproduce the formula),

    Omitting the "Value" line from Test3 gives an instant result with the relative formulae as required.
    So in principle that's the solution I am looking for, but it has a preconception of the simple cell formula in my example.

    However, the formulae I will ultimately be running will be different in each starting cell and are much more complex,
    so I need to turn this into a generic solution which simply reproduces whatever formula is in the starting cells.

    Could it be that I need simply to replace "+A11" in your formula with any formula as in the starting cell?
    And as each starting cell has a different formula perhaps I should to run it separately for each?

    A

  6. #6
    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,457

    Re: Copy formulae to range without Copy/Paste

    I can only repeat, there is a problem with your system. All three versions should be virtually instantaneous.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Do you have other workbooks open? Do you have complex formulae in those workbooks? When you say that you ran the code in an new empty workbook, do you have other workbooks open?

    You could try this way to miminise the impact of other workbooks and formulae.

    Please Login or Register  to view this content.
    Output:
    Please Login or Register  to view this content.

    Regards, TMS

  7. #7
    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,457

    Re: Copy formulae to range without Copy/Paste

    In the top (left) cell, you would put the formula as it appears in that cell; it will be replicated in cells down the column (and across), just as if you had dragged it.

  8. #8
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Re: Copy formulae to range without Copy/Paste

    Thanks TMS

    Sorry for delay in response (called away)

    I have no other workbooks open, and am using just the one test sheet.

    Using your code I get the same times as previously.

    For comparison I have tried it on my laptop and the results are better, 1 sec instead of 2 sec. for copy/paste.

    As there is apparently no macro capable of reproducing the copy/paste function without using the clipboard, I have a workaround.

    Range("A1:A20").Formula =Range("A1").Formula
    etc for each column, then clear cells A2 to D9.

    This works instantly and gives the correct results.

    Many thanks for your time and effort.

    A

  9. #9
    Forum Contributor
    Join Date
    02-12-2006
    Location
    London UK
    Posts
    109

    Re: Copy formulae to range without Copy/Paste

    Postscript

    I have noticed that there is no delay when using copy/paste on text (rather than formulae).

    So if any formulae in cells A2:D9 need to be retained rather than cleared, they can be converted to text (using CtrlH, replacing = with #) and copy/pasted to a blank area before running
    Range("A1:A20").Formula =Range("A1").Formula
    for each column. Then the text cells can be copy/pasted back to A2:D9 and refomatted to formulae (using CtrlH, replacing # with =).

    This runs instantly, and the result is correct.

    A

+ 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] Can I copy and paste formulae out of multiple cells into, say, Notepad?
    By seanspotatobusiness in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-26-2014, 10:44 AM
  2. [SOLVED] copy & paste between worksheets, formulae still refer to first worksheet
    By johnandrews in forum Excel General
    Replies: 5
    Last Post: 12-20-2013, 11:22 AM
  3. Copy/Paste range of data excluding certain rows and copy to another workbook
    By HoerbigAdm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-12-2012, 02:51 PM
  4. [SOLVED] Formulae, borders, fill color do not copy/paste like they used to
    By swampmagic in forum Excel General
    Replies: 3
    Last Post: 05-02-2012, 09:58 AM
  5. Copy n Paste formulae with dual screens
    By toeragh in forum Excel General
    Replies: 2
    Last Post: 02-02-2010, 08:20 AM
  6. How can I autofill (copy & paste) formulae down rows in a spreadsheet?
    By Trevorst in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 11-02-2008, 05:11 AM
  7. [SOLVED] formulae to copy cells from 1 worksheet and paste into another?
    By MikeR-Oz in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 03-23-2006, 07:45 AM
  8. Copy formulae and paste to range with Office 2003
    By Excel4Ever in forum Excel General
    Replies: 0
    Last Post: 11-21-2005, 07:00 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