Solve operators within a string...split?

G

Guest

I have a string that contains math problems with numbers and operators. I
need to assign the solution to a variable. See examples below.

My first thought was to use split with "+" as my delimiter. Worked great!
Problem is that the string can have any combination of +, -, *, and /
operators or no operators at all.

Anyone have a suggestion?

Examples:

String ; Result
"15" ; 15
"10+5" ; 15
"20-5" ; 15
"10+7-2" ; 15
"30/3+5" ; 15
"-5+20" ; 15

Notice the minus sign in the last example. If a minus sign is the first
character then the first number is negative.
 
B

Bob Phillips

Evaluate it

?evaluate(activecell.value)
in the immediate window

or

MsgBox evaluate(activecell.value)
 
T

Tom Ogilvy

Just to elaborate - any valid Excel formula:

sStr = "cos(45)/sin(23)^3"
? evaluate(sStr)
-0.866912172733401
 
G

Guest

My God! Was it really that simple? I can't believe it. Thanks so much Bob
for your response. Not only do I have my fix, but I have a new method under
my belt as well.

Thanks,
Mike
 
B

Bob Phillips

Mike,

As Tom points out, Evaluate can be used for any valid formula, like

?evaluate("cos(45)/sin(23)^3")
 

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