
'****************************************************************
' Math Expression Evaluation Function. r1.1 , April 2003
' using the standard Application.Evaluate EXCEL object
' It work with both numeric/symbolic expression
' variable symbols are: x, y, z
'function recognized are:
'abs,atan,cos,exp,round,int,ln,log,rand,sign,sin,sqrt,tan,acos,
'asin,cosh,sinh,tanh,acosh,asinh,atanh,fact,min,max,mod
'****************************************************************
Function EvalFormula(Formula, Optional x, Optional y, Optional z)
' Math Expression Evaluation Function. r1.0 , Sept 2001
' using the standard Application.Evaluate EXCEL object
' It work with both numeric/symbolic expression
' variable symbols are: x, y, z
' formula= any expr string like "x^2+3*x+2", "sin(x^2+y^2)"
' returns a numeric evaluation or an error message
'Error 2029 function not found
'Error 2036 evaluate impossible
'Error 2007 division for 0
'Error 2036 evaluate impossible
'Error 2015 syntax error
'----------------------------------------------------------------
f$ = Formula
If Not IsMissing(x) Then
VarValue$ = "(" + Trim(Str(x)) + ")"
strSubstitute f$, "x", VarValue$
End If
If Not IsMissing(y) Then
VarValue$ = "(" + Trim(Str(y)) + ")"
strSubstitute f$, "y", VarValue$
End If
If Not IsMissing(z) Then
VarValue$ = "(" + Trim(Str(z)) + ")"
strSubstitute f$, "z", VarValue$
End If
EvalFormula = Application.Evaluate(f$)
End Function
Private Sub strSubstitute(str1, str2, str3)
'subsitutes string str2 with str3 into str1
'ver. 3-4-2003 X
Dim p%
p = 0
Do
p = InStr(p + 1, str1, str2, vbTextCompare)
If p = 0 Then Exit Do
'---- --- fix. bug for exp() and max(). Thanks to Chi M Le
C1 = "-": C2 = "-"
If p > 1 Then C1 = Mid(str1, p - 1, 1)
If p < Len(str1) Then C2 = Mid(str1, p + 1, 1)
If Not (IsLetter(C1) Or IsLetter(C2)) Then
'-------------------------------
S1$ = Left(str1, p - 1)
L% = Len(str1) - p - Len(str2) + 1
S2$ = Right(str1, L%)
str1 = S1$ + str3 + S2$
End If
Loop
End Sub
Private Function IsLetter(c) As Boolean
code = Asc(c)
If (65 <= code And code <= 90) Or _
(97 <= code And code <= 122) Then
IsLetter = True
Else
IsLetter = False
End If
End Function
|
'****************************************************************
' Math Expression Evaluation Function. r1.1 , April 2003
' using the standard Application.Evaluate EXCEL object
' It work with both numeric/symbolic expression
' variable symbols are: x, y, z
'function recognized are:
'abs,atan,cos,exp,round,int,ln,log,rand,sign,sin,sqrt,tan,acos,
'asin,cosh,sinh,tanh,acosh,asinh,atanh,fact,min,max,mod
'****************************************************************
Function EvalFormula(Formula, Optional x, Optional y, Optional z)
' Math Expression Evaluation Function. r1.0 , Sept 2001
' using the standard Application.Evaluate EXCEL object
' It work with both numeric/symbolic expression
' variable symbols are: x, y, z
' formula= any expr string like "x^2+3*x+2", "sin(x^2+y^2)"
' returns a numeric evaluation or an error message
'Error 2029 function not found
'Error 2036 evaluate impossible
'Error 2007 division for 0
'Error 2036 evaluate impossible
'Error 2015 syntax error
'----------------------------------------------------------------
f$ = Formula
If Not IsMissing(x) Then
VarValue$ = "(" + Trim(Str(x)) + ")"
strSubstitute f$, "x", VarValue$
End If
If Not IsMissing(y) Then
VarValue$ = "(" + Trim(Str(y)) + ")"
strSubstitute f$, "y", VarValue$
End If
If Not IsMissing(z) Then
VarValue$ = "(" + Trim(Str(z)) + ")"
strSubstitute f$, "z", VarValue$
End If
EvalFormula = Application.Evaluate(f$)
End Function
Private Sub strSubstitute(str1, str2, str3)
'subsitutes string str2 with str3 into str1
'ver. 3-4-2003 X
Dim p%
p = 0
Do
p = InStr(p + 1, str1, str2, vbTextCompare)
If p = 0 Then Exit Do
'---- --- fix. bug for exp() and max(). Thanks to Chi M Le
C1 = "-": C2 = "-"
If p > 1 Then C1 = Mid(str1, p - 1, 1)
If p < Len(str1) Then C2 = Mid(str1, p + 1, 1)
If Not (IsLetter(C1) Or IsLetter(C2)) Then
'-------------------------------
S1$ = Left(str1, p - 1)
L% = Len(str1) - p - Len(str2) + 1
S2$ = Right(str1, L%)
str1 = S1$ + str3 + S2$
End If
Loop
End Sub
Private Function IsLetter(c) As Boolean
code = Asc(c)
If (65 <= code And code <= 90) Or _
(97 <= code And code <= 122) Then
IsLetter = True
Else
IsLetter = False
End If
End Function
|
