+ Reply to Thread
Results 1 to 9 of 9

Copy formula with unchanged references

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2006
    Location
    English(UK)
    MS-Off Ver
    2019
    Posts
    93

    Copy formula with unchanged references

    You know - just the expression.
    For example things like "=1+A1" or "=A1*B1"
    Right now it changes the cells even with Paste Special!
    Last edited by martix; 05-13-2009 at 08:38 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    re: Copy formula with unchanged references

    See Help for About cell and range references
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-06-2006
    Location
    English(UK)
    MS-Off Ver
    2019
    Posts
    93

    re: Copy formula with unchanged references

    But how do I do it en masse? Without changing any of the references anyhow...

    Here is what I figured just now - select what is to be copied; Replace [=] with ['=]; do the copy; replace back.
    Do you mean to tell me that there is no other way to get the same results without all those steps?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    re: Copy formula with unchanged references

    If you use absolute references, they won't change.

  5. #5
    Registered User
    Join Date
    06-06-2006
    Location
    English(UK)
    MS-Off Ver
    2019
    Posts
    93

    re: Copy formula with unchanged references

    Well I'd have to edit an already large enough dataset. Sure Excel has some form of regexes, but they are very awkward to deal with.
    And still I end up with the replace function. :P
    So can I assume that the answer to my previous question is - Yes, there is no straightforward way.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    re: Copy formula with unchanged references

    I have a function that does it for a single cell -- I use it when I want to try variations of formulas, and want to make sure that the variation gives the same result.

    The reason that Excel doesn't have any method (that I know of) is that people rarely want to see the same thing calculated with the same values in more than one place. Why do you?

    You could use a a sub:
    Sub FormulaCopy(rInp As Range, rOut As Range)
        ' Copies formula in rInp to rOut without adjustments to references
        rOut.Resize(rInp.Rows.Count, rInp.Columns.Count).Value = rInp.Formula
    End Sub
    Then from the Immediate window, for example,

    FormulaCopy range("B1:B10"), range("D3")

  7. #7
    Registered User
    Join Date
    06-06-2006
    Location
    English(UK)
    MS-Off Ver
    2019
    Posts
    93

    re: Copy formula with unchanged references

    For the exact same reason as you... For example I want to see if a pattern is preserved on other arguments/constants or the relation between the two sets of data.
    Only that I use array formulas quite often.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    re: Copy formula with unchanged references

    Here are the functions I use to copy a single formula:
    Sub CopyFormula()
        ' shortcut Ctrl+Shift+C
        Dim oDO     As DataObject
    
        Set oDO = New DataObject
        oDO.SetText ActiveCell.Formula
        oDO.PutInClipboard
    End Sub
    
    Sub PasteFormula()
        ' shortcut Ctrl+Shift+V
        Dim oDO     As DataObject
        Dim cell    As Range
    
        If TypeName(Selection) <> "Range" Then
            MsgBox "Selection must be a Range!", "PasteFormula"
            Exit Sub
        End If
        
        Set oDO = New DataObject
        
        On Error Resume Next
        oDO.GetFromClipboard
        
        For Each cell In Selection
            cell.Formula = oDO.GetText
        Next cell
    End Sub
    You need a set a reference to Microsoft Forms Object Library.

    If you're copying array formulas, you have to remake them as array formulas -- I never had occasion to need to do so, so the code doesn't.
    Last edited by shg; 05-13-2009 at 08:13 PM.

  9. #9
    Registered User
    Join Date
    06-06-2006
    Location
    English(UK)
    MS-Off Ver
    2019
    Posts
    93

    Re: Copy formula with unchanged references

    I see... well thanks

+ 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