View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default extract text from string, leaving numbers and operators

On Fri, 20 Feb 2009 09:57:02 -0800, CEG wrote:

I have a column containing text and numbers with operators, such as:
"4 weeks * 40 hours per week." or
"4 wks * 40 hrs per week = 160 hours." or
"2 wks * 40 hrs per week + 2 wks * 20 hrs per week."

I need to strip out the text and end up with a formula, such as "4*40",
resulting in 160.

Need to recognize +, -, *, /. And need to ignore anthing after =, if there
is one.

The objective is to take what is sometimes a long text string and check the
math in the string.

I'm guessing this will take some fancy code. Any help greatly appreciated.



==================
Option Explicit
Function foo(sStr)
Dim i As Long
Dim sTemp As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "[^-\d+*/^=]"
sTemp = re.Replace(sStr, "")
re.Pattern = "=.*"
foo = Evaluate(re.Replace(sTemp, ""))
End Function
==========================
--ron