获取目标单元格中另一个单元格的公式
一个单元格如何在不使用VBA的情况下以文本形式获取另一个单元格的公式?我可以看到这个问题已经被问过很多次了,答案总是在 VBA 中编写自定义函数。
然而,我发现了一个 2006 年发表的帖子,声称找到了非 VBA 解决方案,但该帖子中提供的链接已损坏。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
=FormulaText(Reference)
就可以了 文档=FormulaText(Reference)
will do the trick Documentation有一个很好的方法可以在不使用 VBA 的情况下完成此操作。它使用 XL4 宏(这些是宏,但不是 VBA,如要求的那样)。
参考图1,单元格A2:A4包含常用公式。
转到公式 ->定义名称,我定义了两个命名范围(见图2),信息显示在单元格A6:B8中。
在单元格 B2 中输入
=FormulaAsText
。这将以文本形式检索单元格 A2 中的公式。说明:
命名范围
FormulaAsText
使用=GET.CELL(info_type,reference)
。在本例中,ìnfo_type = 6
检索公式,reference = OFFSET(INDIRECT("RC",FALSE),0,-1)
使用包含 0 行的单元格-1 列与使用公式的列偏移。复制 B2 并粘贴到 B3:B4 中。这将显示 A3:A4 中的公式。单元格 A4 显示工作表函数
CELL
仅检索值,而不检索公式(与GET.CELL
相反)。由于
FormulaAsText
从当前单元格的固定偏移量 (0,-1) 处获取公式,因此我定义了另一个范围FormulaAsText2
,它使用偏移量(行,cols)从工作表本身读取。单元格 D2:D4 包含=FormulaAsText2
。因此,单元格 D2 显示单元格 B3 的内容 (=OffSET(D2,1,-2)
),即FormulaAsText
。单元格 D3:D4 显示其自身的内容。这增加了一些灵活性。 YMMV。PS1:精华取自
http://www.mrexcel。 com/forum/excel-questions/20611-info-only-get-cell-arguments.html
PS2:蒂姆·威廉姆斯在评论中提到“旧的 XLM
GET.FORMULA()
”。这个答案可能是相关的(不一样,因为这个答案使用GET.CELL()
)。PS3:给出了一个简单的VBA解决方案,例如,
http://dmcritchie.mvps.org/excel/formula.htm
编辑:补充这个不错的答案,工作表函数
FormulaText
适用于 Excel 2013< /a> 及以后。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 usesGET.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.对于那些在检索公式块并将其传输到新电子表格后想要再次使用它们的人来说,此建议可能会有所帮助。 Excels FORMULATEXT 函数非常适合提取公式,但它会将它们保留为不可用的文本字符串。如果您想将它们恢复为功能齐全的公式,则必须单独编辑每个公式以删除字符串字符,但这是较大块的快捷方式。
到达所需公式作为文本的位置(换句话说,在使用 FORMULATEXT 之后 - 您已完成复制和(仅值)粘贴)。下一步涉及突出显示要转换的所有单元格,然后导航到 [文本到列] 菜单选项(Excel 2016 上的 {数据} 栏)。您可以选择“分隔”,但在下一个屏幕上只需确保取消选择公式中出现的任何标记即可。然后“完成”。 Excel 会自动分析包含公式的单元格,您现在应该可以让它们再次工作。
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.
有一种方法可以做到这一点。在我的示例中,我有一个显示日期的表格。该日期来自 Sheet!G91。在我的表格中,我还有一列显示工作表名称。我在表中又添加了两列。第一列有 column(Sheet!g91),它返回数字 7,因为 G 是字母表中的第七个字母。然后,我使用工作簿中的另一个表格将数字转换为字母 (G)。在我添加的第二列中,我创建了一个公式行(Sheet!G91),它返回数字 91。 注意:行和列可能显示为易失公式,它们会在工作簿的每次计算中重新计算。
我想要另一列来显示本文开头提到的日期单元格的公式内容。我包含了以下字符串函数(您也可以使用 CONCATENATE)。
“=”& AJ9& “!” & AM9& AN9
由 & 符号分隔的项目串在一起(即串联)。我的示例中的 AJ9 包含工作表名称,AM9 包含列字母,AN9 包含行号。
我现在有一个列,可以动态更新其内容以反映工作表名称和单元格引用。我的工作簿单元格中的结果是
=Sheet!G91。
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.
你不能。这很可能是一种设计选择,旨在防止普通 Excel 用户意外获得他们不想要的东西。
您正在阅读的内容是正确的 - 编写 UDF 是您想要的解决方案。
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.