2016-06-29 7 views
2

私はExcelで計算を実行して表示するのが理想的です。この作品Excelで計算、値、および変数を表示する方法は?

Function DisplayFormula(range_rng As Range) As String 
    Application.Volatile 

    If range_rng.HasArray Then 
     DisplayFormula = "<-- " & " {" & range_rng.FormulaArray & "}"  
    Else 
     DisplayFormula = "<-- " & " " & range_rng.FormulaArray 
    End If 
End Function 

、しかし、私は2つの変更の実装にこだわっている:私は、次のVBAワークシート関数を使用して計算を表示するには

  1. 私は、実際の値を表示したいことrange_rngで呼び出されます。
  2. 私は範囲の代わりに変数を表示したいと思います。変数は、呼び出されたセルの隣の別のセルに割り当てられます(下の図を参照)。

列 "C" はDisplayFormulaのための(希望)出力形式(B3)を示しています

This is how the output should look like.

+0

配列を使用してarr()= range_rng.value2の配列を使用し、このバックアップに参加して文字列を返します。 –

+0

range_rng.value2を使用すると、range_rngの出力が返されます(例:12.5)。私は計算を返したいと思います(例えば上記の5/2 + 10)。どうもありがとう! – CFW

答えて

1

あなたはこの強引なアプローチを試すことができます。
これは最適化されているとは言えませんが、上記の2つの条件を満たすことができます。

Function DisplayFormula2(r As Range, Optional o As Variant) As String 
    Dim a, b, z, x, y, w 
    Dim f As String, tf As String 
    Dim c As Range 
    Dim i As Integer 

    If IsMissing(o) Then o = 0 

    a = Array("+", "-", "/", "*", "%", "&", "^", "=", _ 
     "<", ">", "<=", ">=", "<>", "(", ")") 

    f = r.FormulaArray: tf = f 
    For Each b In a 
     With Application.WorksheetFunction 
      tf = .Substitute(tf, b, "|") 
     End With 
    Next 

    z = VBA.Split(tf, "|") 

    For Each w In z 
     Debug.Print w 
     On Error Resume Next 
     Set c = Range(w) 
     On Error GoTo 0 
     If Not c Is Nothing Then 
      If IsArray(x) Then 
       ReDim Preserve x(UBound(x) + 1): x(UBound(x)) = w 
       ReDim Preserve y(UBound(y) + 1): y(UBound(y)) = c.Offset(0, o).Value2 
      Else 
       x = Array(w) 
       y = Array(c.Offset(0, o).Value2) 
      End If 
     End If 
     Set c = Nothing 
    Next 

    If IsArray(x) Then 
     For i = LBound(x) To UBound(x) 
      With Application.WorksheetFunction 
       f = .Substitute(f, x(i), y(i)) 
      End With 
     Next 
    End If 
    DisplayFormula2 = IIf(r.HasArray, "<-- {" & f & "}", "<-- " & f) 
End Function 

ところで、私は.Volatileを使用する必要はないと思って削除しました。
計算モードを[自動]に設定している間は、再計算されます。 C3で

実際の式:C5:
C3:=DisplayFormula(B3)
C4:=DisplayFormula2(B4)
C5:=DisplayFormula2(B5,-1)

0

あなたはテキスト形式に、標的細胞を変更していることを達成することができます。これを試してください:

Function DisplayFormula(range_rng As Range) As String 
    Application.Volatile 
    ActiveCell.NumberFormat = "@" 

    If range_rng.HasArray Then 
     DisplayFormula = "<-- " & " {" & range_rng.FormulaArray & "}" 
    Else 
     DisplayFormula = "<-- " & " " & range_rng.FormulaArray 
    End If 
End Function 
関連する問題