urgent question: how to calculate value with a string formula

G

Guest

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!
 
J

JulieD

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
 
T

Tom Ogilvy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top