【发布时间】:2019-10-29 07:07:54
【问题描述】:
我正在尝试将函数创建的公式转换为作为公式而不是函数括号返回。如附件截图所示。
Function f2t2(rng As Range) As String
Application.ScreenUpdating = False
jGetFormula = rng.Formula
jGetFormula = Replace(jGetFormula, "(", """" & "(" & """" & "&")
jGetFormula = Replace(jGetFormula, ")", "&" & """" & ")" & """" & "&")
jGetFormula = Replace(jGetFormula, "+", "&" & """" & "+" & """" & "&")
jGetFormula = Replace(jGetFormula, "-", "&" & """" & "-" & """" & "&")
jGetFormula = Replace(jGetFormula, "*", "&" & """" & Chr(215) & """" & "&")
jGetFormula = Replace(jGetFormula, "/", "&" & """" & "/" & """" & "&")
jGetFormula = Replace(jGetFormula, "^", "&" & """" & "^" & """" & "&")
jGetFormula = Replace(jGetFormula, "&&", "&")
If (Right(jGetFormula, 1) = "&") Then
jGetFormula = Left(jGetFormula, (Len(jGetFormula) - 1))
End If
'MsgBox jGetFormula
'recalcualting other formulas in the excel
Application.Volatile
'Returning to excel
f2t2 = jGetFormula
'f2t = jGetFormula
Application.ScreenUpdating = True
Application.StatusBar = ""
End Function
我正在尝试将函数创建的公式转换为作为公式而不是函数括号返回。如附件截图所示:
Sub Formula_Edit(Optional endAll As Boolean = False)
MsgBox "3"
Range("T101").Value = 5
If endAll Then End
MsgBox "4"
End Sub
Function call2()
MsgBox "1"
Call Formula_Edit(True)
MsgBox "2"
End Function
【问题讨论】:
-
您不能使用 UDF(用户定义函数)将公式返回到单元格中。函数只能返回值,不能返回公式。这意味着您的函数
f2t2可以将结果作为文本或数字返回,但它不能将公式写入单元格。 -
感谢 PEH 的快速响应,基本上我想显示公式中使用的值,即我想显示 1×1.4+2-(1.4/2) 结果 2.7,所以有什么办法周围做这个。
-
您不只是在寻找从 Excel 2013 开始提供的
FORMULATEXT函数吗? -
@JvdV 不,基本上他想做一个公式来文本并用实际值替换地址。
-
@Pᴇʜ,对,那么更有可能寻找
Range.Precedents?
标签: excel vba function excel-formula