ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   urgent question: how to calculate value with a string formula (https://www.excelbanter.com/excel-programming/306386-urgent-question-how-calculate-value-string-formula.html)

miao jie

urgent question: how to calculate value with a string formula
 
In Excel, I can get a string formula for some place.
for example: (P1+P2+P3^2)/3 , P1*2+P4/3, ........

P1, P2,P3,P4 is variant I can its value from some place:
for example: P1 = 1, P2 =2, P3 =3, P4 =4

so I can get the calculated formula as follow: "(1+2+3^2)/3"
my question is: how to convert this string formula to real calculate formula
? so I can get value (1+2+3^2)/3 is 4 ??

I have review all of function of string, can not find a way to do it:(
anyone can help me out ?? thanks a lot!

JulieD

urgent question: how to calculate value with a string formula
 
Hi

not sure i'm understanding you correctly
but
if you type
=(P1+P2+P3^2)/3
into a cell (with the =)
you will have a calulation that can be evaluated.

if this isn't what you're after please post back.
Regards
JulieD

"miao jie" wrote in message
...
In Excel, I can get a string formula for some place.
for example: (P1+P2+P3^2)/3 , P1*2+P4/3, ........

P1, P2,P3,P4 is variant I can its value from some place:
for example: P1 = 1, P2 =2, P3 =3, P4 =4

so I can get the calculated formula as follow: "(1+2+3^2)/3"
my question is: how to convert this string formula to real calculate

formula
? so I can get value (1+2+3^2)/3 is 4 ??

I have review all of function of string, can not find a way to do it:(
anyone can help me out ?? thanks a lot!




Tom Ogilvy

urgent question: how to calculate value with a string formula
 
Sub Tester12()
sForm = "(P1+P2+P3^2)/3"
p1 = 2
p2 = 3
p3 = 4
p4 = 0
Debug.Print EvalFormula(p1, p2, p3, p4, sForm)
End Sub

Public Function EvalFormula(p1, p2, p3, p4, sForm)
Dim sForm1 As String
sForm1 = UCase(sForm)
sForm1 = Application.Substitute(sForm1, "P1", p1)
Debug.Print sForm1, p1
sForm1 = Application.Substitute(sForm1, "P2", p2)
sForm1 = Application.Substitute(sForm1, "P3", p3)
sForm1 = Application.Substitute(sForm1, "P4", p4)
EvalFormula = Evaluate(sForm1)
End Function

--
Regards,
Tom Ogilvy

"miao jie" wrote in message
...
In Excel, I can get a string formula for some place.
for example: (P1+P2+P3^2)/3 , P1*2+P4/3, ........

P1, P2,P3,P4 is variant I can its value from some place:
for example: P1 = 1, P2 =2, P3 =3, P4 =4

so I can get the calculated formula as follow: "(1+2+3^2)/3"
my question is: how to convert this string formula to real calculate

formula
? so I can get value (1+2+3^2)/3 is 4 ??

I have review all of function of string, can not find a way to do it:(
anyone can help me out ?? thanks a lot!





All times are GMT +1. The time now is 12:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com