开发者

Using a formula from another column in Excel

开发者 https://www.devze.com 2022-12-08 20:30 出处:网络
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

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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消