[英]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.