Getting formula of another cell in target cell
There is nice way of doing this without VBA. It uses XL4 macros (these are macros, but it is not VBA, as asked).
With reference to the figure 1, cells A2:A4 contain usual formulas.
Going to Formulas -> Define Name, I defined two named ranges (see fig. 2), with the information shown in cells A6:B8.
Enter in cell B2
=FormulaAsText
. This will retrieve the formula in cell A2 as text.Explanation: The named range
FormulaAsText
uses=GET.CELL(info_type,reference)
. In this case,ìnfo_type = 6
retrieves the formula, andreference = OFFSET(INDIRECT("RC",FALSE),0,-1)
uses the cell with 0 rows and -1 columns offset from the one the formula is used in.Copy B2 and paste into B3:B4. This will show formulas in A3:A4. Cell A4 shows that the worksheet function
CELL
only retrieves values, not formulas (as opposed toGET.CELL
).Since
FormulaAsText
gets the formula from a cell at fixed offset (0,-1) from the current, I defined another rangeFormulaAsText2
, which uses an offset (rows,cols) read from the worksheet itself. Cells D2:D4 contain=FormulaAsText2
. Thus, cell D2 shows the contents of cell B3 (=OffSET(D2,1,-2)
), which isFormulaAsText
. cells D3:D4 show the contents of themselves. This adds some flexibility. YMMV.
PS1: The essence was taken from http://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
PS2: Tim Williams mentioned in a comment "the old XLM GET.FORMULA()
". This answer is possibly related (not the same, since this one uses GET.CELL()
).
PS3: A simple VBA solution is given, e.g., in http://dmcritchie.mvps.org/excel/formula.htm
EDIT: Complementing this nice answer, the worksheet function FormulaText
is available for Excel 2013 and later.
=FormulaText(Reference)
will do the trick Documentation