Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default 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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default 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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default 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.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default 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.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula Help - Split capital letters from first part of string Beverly-Texas Excel Discussion (Misc queries) 2 February 4th 10 09:06 PM
Split text string using mid-search? DubboPete Excel Discussion (Misc queries) 3 January 28th 10 12:37 AM
extract text from string, leaving numbers and operators CEG Excel Discussion (Misc queries) 13 March 4th 09 11:31 PM
split string based on a SET of words [email protected] Excel Discussion (Misc queries) 8 August 3rd 07 03:09 AM
Excel 2000 VBA - Using Split() with no Lelimiter in string sub_pop[_8_] Excel Programming 4 July 29th 04 10:32 PM


All times are GMT +1. The time now is 11:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"