Search code examples
excelvba

Is it possible to change the content of a cell with VBA to make it a formula?


I've begun using macros last week so I'm really a beginner. I have a page containing a lot of formulas and I want to have another one containing the same things except I would need them to be conserved as something else than formulas to save memory.

For now every cell that should contain a formula is formulated like this :

§=EQUIV(F7;INDIRECT(A2);0)

I tried recording a macro in which I replace every "§" by "", but it doesn't work. After a few other tests I determined that I could not replace characters that would turn a cell into a formula this way, but maybe I am missing something ?


Solution

  • In this case you can use this code:

    Sub ToFormula()
       With Range("A1")
          .FormulaLocal = Replace(.Value, Chr(167), vbNullString)
       End With
    End Sub
    

    A1 is an address of the cell containing formula to change.
    If there are more cells to convert you can use Excel Substitute function:

    Sub ToFormula1()
       With Range("A1:A3")
          .FormulaLocal = Application.Substitute(.Value, Chr(167), vbNullString)
       '  another way
       '  .FormulaLocal = Application.Replace(.Value, 1, 1, vbNullString)
       End With
    End Sub