views:

255

answers:

2

I'll simplify this for example sake

I have a formula in column B which is Ax + 2,

e.g. in B1, it's A1 + 2 in B2, it's A2 + 2

I want to make a formula in column Y which mirrors whatever formula is in column B at all times but replaces the A in the formula with a X.

e.g. in Y1, it's X1 + 2 in Y2, it's X2 + 2

No matter what happens to the formula in column B, I want the formula in Y to reflect those changes.

Effectively, if I could do something akin to =B1.formula.replace("A","X") then that would do the job.

Can this be done in the formula bar at the top or will it need to be done via a macro?

Thanks

+1  A: 

Select B, copy the column, select Y and "paste special" choosing formula only should do the job

Following your comment:

Private Sub Workbook_Open()
   Range("Y:Y").FormulaR1C1 = Range("B:B").FormulaR1C1
End Sub

will do the job (macro to put in ThisWorkbook)

Following your second comment:

Sub Workbook_Open()
    On Error GoTo errLbl
    xlCalc = Application.Calculation
    Application.Calculation = xlCalculationManual ' stop calculation '
    Application.ScreenUpdating = False ' disable screen update '

    For Each c In Range("B:B")
        Range("Y" & c.Row).Formula = Replace(c.Formula, "A", "D")
        If c.Formula = vbNullString Then Exit For ' stop if "B" has no formula '
    Next

errLbl:       
    Application.ScreenUpdating = True ' enable screen update '
    Application.Calculation = xlCalc ' enable calculation back to where it was '
End Sub
RC
Thanks, that's a good solution but would mean that once the copy is made, there is no reference back to the original formula so if that changes, then column Y formula won't reflect the change. Is it possible to dynamically reference the formula in column B?
Graeme
Yes the copy method is a one shot, I edited to add a dynamic way.
RC
That's a good idea doing that on the Workbook open - how would I do the replacement of the formula? When I did the code above, the formula became translated to another column and so I'd like to change the column Q to be column X. Can Replace be used when referring to Ranges?
Graeme
the code I posted use formula in R1C1 coordinates i.e. each reference in the formula is in the form "N col/row before/after" (in the example you provided "B" = "A" + 2 is then interpreted as "B" = previous col + 2) If you need something more complicated (ex. "Y" = next column + 2), you will need to use the replace posted above
RC
Ah I see, yeah sorry my simple example was a bit offputting as the column locations are different distances apart. I can fix this though and probably will just to make it easier!Thanks
Graeme
+1  A: 

Here is the solution

Sub Button1_Click() Dim s As String

s = Range("b2").Formula

Dim res As String res = Replace(s, "A", "x")

MsgBox res

End Sub

amr osama
Thanks, I started going down this route but then noticed the answer above which suits me better since it will happen everytime I open the spreadsheet
Graeme
also try this event -> Worksheet_Calculate()
amr osama
Even better thanks!Do you know the answer to my question above?I'm able to replicate the formula automatically from one column to the other but to actually then substitute one of the column refs in the formula to another one, how do I do that?Do I just have to do a for loop down the rows replacing it or is there a way I can do this on a Range?Thanks
Graeme
I think for loop is okay for this task
amr osama