简体   繁体   English

Function 返回值为 excel 公式

[英]Function return value as excel formula

I am trying to convert a formula created by function to return as a formula instead of function brackets.我正在尝试将 function 创建的公式转换为公式而不是 function 括号。 As shown in screenshot attached.如随附的屏幕截图所示。

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

I am trying to convert a formula created by function to return as a formula instead of function brackets.我正在尝试将 function 创建的公式转换为公式而不是 function 括号。 As shown in screenshot attached:如附件截图所示:

截屏

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

As JvdV pointed out there is the Range.Precedents property which returns all cells the range depends on.正如 JvdV 指出的那样,有一个Range.Precedents 属性,它返回范围所依赖的所有单元格。 So we can loop through these cells and take their addresses to replace them with values (see below example).因此,我们可以遍历这些单元格并获取它们的地址以将它们替换为值(参见下面的示例)。

Sub test()
    Dim rng As Range
    Set rng = Range("H19")

    Dim Output As String
    Output = "'" & rng.Formula
    Output = Replace$(Output, "*", "×")

    Dim r As Range
    For Each r In rng.Precedents
        Output = Replace$(Output, r.Address(RowAbsolute:=False, ColumnAbsolute:=False), r.Value) 'H16
        Output = Replace$(Output, r.Address(RowAbsolute:=True, ColumnAbsolute:=False), r.Value)  'H$16
        Output = Replace$(Output, r.Address(RowAbsolute:=False, ColumnAbsolute:=True), r.Value)  '$H16
        Output = Replace$(Output, r.Address(RowAbsolute:=True, ColumnAbsolute:=True), r.Value)   '$H$16
    Next r

    Range("H20").Value = Output
End Sub

Procedure 1: This can convert simple formulas but you cannot use it in a UDF!程序 1:这可以转换简单的公式,但您不能在 UDF 中使用它!

But this does not work in a user defined function, only in a procedure that is eg called by a button or shortcut.但这在用户定义的 function 中不起作用,仅在通过按钮或快捷方式调用的过程中起作用。 Also this does only work for simple formulas like you showed.这也只适用于你展示的简单公式。

For example it can convert例如它可以转换

=H16*H17+H18-(H17/H18)

into进入

'=1×1.4+2-(1.4/2)

But if you have a more complicated formula like但是如果你有一个更复杂的公式,比如

=Sheet2!H16*Sheet3!H17+H18-(H17/H18)

This approach cannot be used anymore.这种方法不能再使用了。 Also if the formula contains other functions that accept ranges (eg SUM() ), your whole idea cannot work anymore.此外,如果公式包含接受范围的其他函数(例如SUM() ),您的整个想法将不再适用。

Because for example =SUM(H16:H18) cannot be converted into values.因为例如=SUM(H16:H18)无法转换为值。


If you need to do this in a UDF (user defined function) this would only be possible to be solved by parsing the formula.如果您需要在 UDF(用户定义函数)中执行此操作,则只能通过解析公式来解决。 But be aware that this is much more complicated and a way too broad to be answered here.但请注意,这要复杂得多,而且范围太广,无法在这里回答。


Alternative approache that may work: Use named ranges for your values.可能有效的替代方法:为您的值使用命名范围。 For example:例如:

在此处输入图像描述 Image 1: The cell H17 with value 1.4 is named Mass and the cell H18 with value 2 is named SpeedOfLight (named ranges).图 1:值为1.4的单元格 H17 被命名为Mass ,值为 2 的单元格 H18 被命名为SpeedOfLight (命名范围)。

The formula for the cell H19 "Energy" can then be written as =H17*H18^2 or because we use named ranges =Mass*SpeedOfLight^2 .单元格 H19“能量”的公式可以写为=H17*H18^2或者因为我们使用命名范围=Mass*SpeedOfLight^2

Then you can use the FORMULATEXT() function to turn this formula into text and if you like replace * with × .然后您可以使用FORMULATEXT() function 将此公式转换为文本,如果您喜欢将*替换为×

在此处输入图像描述 Image 1: The formula used is: =SUBSTITUTE(FORMULATEXT(H19),"*","×") .图 1:使用的公式是: =SUBSTITUTE(FORMULATEXT(H19),"*","×")

Like already mentioned by Peh, here a solution with parsing the formula.就像 Peh 已经提到的,这里有一个解析公式的解决方案。 This solution may suit your needs but is not full proof.此解决方案可能适合您的需求,但并非完全证明。 Some functions in the functions will be evaluated as one value.函数中的某些函数将被评估为一个值。

Function f2t2(rng As Range) As String
    x = rng.Formula
        For Each del In Array(";", " ", ".", "<", ">", "+", "-", "=", "/", "\", "*", "^") '":","(", ")"
            x = Replace(x, del, "|")
        Next del
            arr1 = Split(x, "|")
            arr2 = arr1

        For i = LBound(arr1) To UBound(arr1)
            On Error Resume Next
            arr2(i) = IIf(Application.Evaluate(arr1(i)) = "", "0", Application.Evaluate(arr1(i)))
            On Error GoTo 0
        Next i

    x = rng.Formula
        For i = LBound(arr1) To UBound(arr1)
            x = Replace(x, arr1(i), arr2(i))
        Next

    f2t2 = x

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM