我使用公式在另一个单元格中显示单元格公式。
我想在此公式中显示每个引用的值,而不是引用。
前:
= $ R $ 16 + R19 *($ T $ 15- $ R $ 16)
希望它是
= 3 + 2 *(4-2)
我正在使用的功能显示公式
Function GetFormula(Cell As Range) As String
GetFormula = Cell.Formula
End Function
答案 0 :(得分:5)
这是一个非常基本的例子,它可以处理像
这样的情况=B1+C1-D1/E1
=$B$1+C1*($B$1-$D$1)
=B1+C1-(D1/E1)
=B1+C1-(D1/E1)
=$B$1*C1*($B$1/$D$1)
<强>假设:强>
示例工作表
<强>代码强>:
Sub Sample()
Dim i As Long
For i = 1 To 5
Debug.Print ConvertToValues(Range("A" & i))
Next i
End Sub
Function ConvertToValues(rng As Range)
Dim sTmp As String, sOpr As String, sOrig As String
Dim s As String, v As String
Dim MyAr
Dim i As Long
sOpr = "+,/,-,*,&,(,),{,},[,]"
MyAr = Split(sOpr, ",")
sTmp = Replace(rng.Formula, "$", "")
sOrig = sTmp
For i = LBound(MyAr) To UBound(MyAr)
sTmp = Replace(sTmp, MyAr(i), "SIDROUT")
Next i
MyAr = Split(sTmp, "SIDROUT")
For i = LBound(MyAr) To UBound(MyAr)
s = MyAr(i)
If Len(Trim(s)) <> 0 Then
v = Range(s).Value
sOrig = Replace(sOrig, s, v)
End If
Next i
If sOrig <> "" Then _
ConvertToValues = "=" & sOrig
End Function
<强>输出强>
注意:
如果上述代码在特定情况下失败,请告诉我,我将更新代码。