0

I want to know how to copy a formula into another, while keeping the original values. For instance, I have in range A1:A5 five values (a mix of constants and/or formulas) and I need (in that same range – not in a new column) to include function =Round("original value or formula",2), (or any other formula; this is just an example)

For example:

  • A1 original value  50.64635
  • A2 original formula =3*F1   (which may display as 54.25875 if F1 contains 18.08625)
  • A3 original formula =SQRT(2)   (which will display as 1.4142136)

Result I want is:

  • A1 new content (formula)  =round(50.64635,2)
  • A2 new content (formula)  =round(3*F1,2)
  • A3 new content (formula)  =round(SQRT(2),2)

etc.  Note that I do not want A1 to be changed to 50.65, and I do not want A2 to be changed to 54.26 or even =ROUND(54.25875,2), etc.  I know how to do similar things with simple operations (copy/paste special – Formula – add, substract, multiply, divide) and keep original values or formulas. But I don't know how to wrap a function around an existing value or otherwise build a formula, using an existing formula as a component.

The other idea would be to play converting my original values/formulas to text, use find/replace function, concatenate, or some weird mix and I would come to result I want, but I would like to know if there is some shorter way ;)

5
  • it's not clear how conditional formatting is related to your question, it doesn't change the value in the cells. Also your question is too broad, we may give hints to specific problems, but there is no way to "batch edit" multiple cells in excel. A solution for your rounding question would be to set number formatting, then in excel options set "set precision as displayed" (other option would be VBA, but that requires learning) Commented Sep 10, 2015 at 11:04
  • @MátéJuhász The OP uses the words "conditional formula". Not sure they are referring to conditional formatting. We'll have to wait for the reply to see what they really mean.
    – CharlieRB
    Commented Sep 10, 2015 at 11:18
  • I think I understand the question, and I edited it in an attempt to clarify it.  Of course, if I got it wrong, edit it back and fix it.  P.S. Nobody knows what you mean by "conditional formula". Commented Sep 10, 2015 at 22:54
  • Thank you for editing Scott. You got it right. By "conditional formula" I meant logical functions or any other function actually.
    – Arom79
    Commented Sep 11, 2015 at 8:57
  • @Arom79: The official way to show thanks is to “accept” an answer by clicking the check mark next to it.  Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. Commented Sep 13, 2015 at 20:52

1 Answer 1

0

Excel's "Find and Replace" capability doesn't seem to be powerful enough to do what you want.  However, it's fairly easy in VBA.


First, make a copy of your file, just in case something goes wrong.

Then create the following VBA subroutine:

Sub Arom79Sub()

    On Error Resume Next
    For Each c In Range("A1:A5")
        If c.HasFormula And Left(c.Formula, 1) = "=" Then
            temp = Right(c.Formula, Len(c.Formula) - 1)
        Else
            temp = c.Value
        End If
        If temp <> "" Then
            c.Formula = "=ROUND(" & temp & ", 2)"
        End If
    Next c
    On Error GoTo 0

End Sub

See How do I add VBA in MS Office? for general information on how to use VBA in Excel and the other Microsoft Office applications.  Arom79Sub is just an arbitrary subroutine name; use whatever name you want.

  • On Error Resume Next says, if an error occurs, just go to the next statement and keep on running.  (For example, if a cell contains a constant value that is not a number (i.e., is a string/text value), the above code will cause an error.)
  • For Each c ... Next c is a loop that looks at every cell in the range A1:A5, setting c to refer to each cell in sequence.
  • c.HasFormula is a Boolean that tells whether cell c contains a formula.  Then we also check to see whether the first character of the "formula" is =. This is probably redundant.  If it's definitely a formula, set temp to the actual formula; i.e., everything to the right of the = (e.g., 3*F1, in your example for A2).
  • Otherwise, set temp to the value in the cell (e.g., 50.64635, in your example for A1).
  • If the value (or formula) is non-blank, set the cell's formula to be
            =ROUND(previous_contents, 2).
  • Do On Error GoTo 0 to restore the normal error handling.

Then run the subroutine.

Obviously you can change the range or the new formula.

1
  • Thank you Scott. Subroutine works perfectly!!. I'll try to play with it to adjust to other functions.
    – Arom79
    Commented Sep 15, 2015 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .