ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Solve operators within a string...split? (https://www.excelbanter.com/excel-programming/329260-solve-operators-within-string-split.html)

crazybass2

Solve operators within a string...split?
 
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.

Bob Phillips[_7_]

Solve operators within a string...split?
 
Evaluate it

?evaluate(activecell.value)
in the immediate window

or

MsgBox evaluate(activecell.value)

--
HTH

Bob Phillips

"crazybass2" wrote in message
...
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.




Tom Ogilvy

Solve operators within a string...split?
 
Demo'd from the immediate window:

sStr = "30/3+5"
? evaluate(sStr)
15

--
Regards,
Tom Ogilvy


"crazybass2" wrote in message
...
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.




Tom Ogilvy

Solve operators within a string...split?
 
Just to elaborate - any valid Excel formula:

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

--
Regards,
Tom Ogilvy

"crazybass2" wrote in message
...
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.




Tom Ogilvy

Solve operators within a string...split?
 
and for your specific point

sStr = "-5+20"
? evaluate(sStr)
15

--
Regards,
Tom Ogilvy


"crazybass2" wrote in message
...
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.




crazybass2

Solve operators within a string...split?
 
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

"Bob Phillips" wrote:

Evaluate it

?evaluate(activecell.value)
in the immediate window

or

MsgBox evaluate(activecell.value)

--
HTH

Bob Phillips

"crazybass2" wrote in message
...
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.





Bob Phillips[_7_]

Solve operators within a string...split?
 
Mike,

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

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


--
HTH

Bob Phillips

"crazybass2" wrote in message
...
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

"Bob Phillips" wrote:

Evaluate it

?evaluate(activecell.value)
in the immediate window

or

MsgBox evaluate(activecell.value)

--
HTH

Bob Phillips

"crazybass2" wrote in message
...
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.








All times are GMT +1. The time now is 01:43 PM.

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