[英]Finding a reference to a cell in an excel formula
是否有公式或宏可以找到对另一个单元格中某个单元格的引用? 例如:
A | B | C
------------------
1| 1 | 2 | =A1+B1
是否有一个公式将C1
作为输入并返回=1+2
?
您可以尝试根据用户要求将此用户定义函数转换为公式中使用的单元格引用为其值。
将以下功能放在诸如Module1之类的标准模块上。
Function CellRefereceToValue(ByVal rng As Range) As String
Dim re As Object, Matches As Object, Match As Object
Dim str As String
Set re = CreateObject("VBScript.RegExp")
With re
.Global = True
.Pattern = "[A-Z]+\d+"
End With
str = rng.Formula
If re.test(str) Then
Set Matches = re.Execute(str)
For Each Match In Matches
str = Replace(str, Match, Range(Match).Value)
Next Match
End If
CellRefereceToValue = str
End Function
然后根据下图假设您的公式在C2中,则在工作表上使用此函数,如下所示...
=CellRefereceToValue(C2)
在下图中,将Excel公式放在C列中,并将公式文本放在D列中,以显示在C列中使用的公式,然后在E列中,您可以看到UDF替换了C列中的公式中使用的所有单元格引用及其相应的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.