Getting formula of another cell in target cell

后端 未结 5 1298
野的像风
野的像风 2020-11-27 20:46

How does one cell obtain the formula of another cell as text without using VBA? I can see this question has already been asked many times and the answer is always to write a

5条回答
  •  情话喂你
    2020-11-27 21:38

    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.

    enter image description here

    1. Going to Formulas -> Define Name, I defined two named ranges (see fig. 2), with the information shown in cells A6:B8.

      enter image description here

    2. 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, and reference = OFFSET(INDIRECT("RC",FALSE),0,-1) uses the cell with 0 rows and -1 columns offset from the one the formula is used in.

    3. 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 to GET.CELL).

    4. Since FormulaAsText gets the formula from a cell at fixed offset (0,-1) from the current, I defined another range FormulaAsText2, 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 is FormulaAsText. 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.

提交回复
热议问题