:
Function ev(f As Variant, A As Range, B As Range, C As Range) As Variant
Dim s As String
s = f
s = Replace(s, "A", "~A~")
s = Replace(s, "B", "~B~")
s = Replace(s, "C", "~C~")
s = Replace(s, "~A~", A.Address)
s = Replace(s, "~B~", B.Address)
s = Replace(s, "~C~", B.Address)
ev = Evaluate(s)
End Function
例えばもしあなた発現はA2であり、A、B、Cの値はC2である:E2
=ev(A2,C2,D2,E2)
戻る計算値
任意の数の変数を許可するには、変数値の代わりにA、B、Cの代わりにParamArrayを使用します。
Aもう少し完全版:
Function ev(expr As Variant, VarNames As Range, varValues As Range) As Variant
Dim s As String
Dim i As Long
s = expr
For i = 1 To VarNames.Columns.Count
s = Replace(s, VarNames.Cells(1, i), "~" & VarNames.Cells(1, i) & "~")
Next
For i = 1 To VarNames.Columns.Count
s = Replace(s, "~" & VarNames.Cells(1, i) & "~", varValues.Cells(1, i).Address)
Next
ev = Evaluate(s)
End Function
は使用方法:
上記と同じデータプラスC1内の変数名:E1
=ev(A2,C1:E1,C2:E2)
あなただけのセルに合計を置くことができ、 '= 5 * A-2 * B-4 * C'のようになります。 –