问题
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 custom function in VBA.
However, I found a post made in 2006 which claimed to have found the non-VBA solution but the link provided in that post is already broken.
回答1:
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.
回答2:
=FormulaText(Reference)
will do the trick Documentation
回答3:
There is a way to do this. In my example I had a table that showed a date. The date comes from Sheet!G91. In my table I also had a column that showed the sheet name. I added two more columns to my table. The first column had column(Sheet!g91), which returns the number 7, because G is the seventh letter in the alphabet. I then converted the number to a letter (G) using another table in my workbook. In the second column that I added, I made a formula row(Sheet!G91), which returns the number 91. Note: Row and Column may appear as volatile formulas, which recalculate with every calculation of the workbook.
I wanted another column to show the formula contents of the date cell mentioned at the beginning of this post. I included the following string function (you can also use CONCATENATE).
"=" & AJ9 & "!" & AM9 & AN9
The items separated by ampersands get strung together (that is, concatenated). AJ9 in my example contains the sheet name, AM9 contains the column letter, and AN9 contains the row number.
I now have a column that dynamically updates its contents to reflect the sheet name and cell reference. The results in my workbook cell are
=Sheet!G91.
回答4:
This suggestion may be helpful for those who after retrieving a block of formulas and transporting them to a new spreadsheet want to put them to work again. Excels FORMULATEXT function is great for picking up formulas but it leaves them as unusable text strings. If you want to get them back as fully functioning formulas you have to edit each one individually to remove the string character, but here is a shortcut for larger blocks. Get to the position where you have the required formulas as text (in other words after using FORMULATEXT - you have done a copy and (value only) paste). The next step involves highlighting all the cells you want to convert and then navigating to the [Text-To-Columns] menu option ({Data} bar on Excel 2016). You can select 'Delimited' but on the next screen just make sure you de-select any marks that do appear in your formulas. Then 'Finish'. Excel should automatically analyse the cells as containing formulas and you should now have them working again.
回答5:
You can't. This is most likely a design choice to eliminate an average Excel user from accidentally getting something they did not want.
What you are reading is correct - writing a UDF is the solution you want.
来源:https://stackoverflow.com/questions/9122046/getting-formula-of-another-cell-in-target-cell