Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default extract text from string, leaving numbers and operators

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.
--
CG
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default extract text from string, leaving numbers and operators

Try this UDF

Function GetResults(Target As String)

Results = ""
For i = 1 To Len(Target)
Select Case Mid(Target, i, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "+", "/",
"-"
Results = Results & Mid(Target, i, 1)
End Select
Next i

GetResults = Evaluate(Results)
End Function


"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.
--
CG

  #3   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default extract text from string, leaving numbers and operators

This works well except when there is a result entered (i.e. 4 * 40 = 160).
If some goober enters 4 * 40 = 140, I want to be able to spot it without my
calculator.

Thanks for your help so far!!!
--
CG


"Joel" wrote:

Try this UDF

Function GetResults(Target As String)

Results = ""
For i = 1 To Len(Target)
Select Case Mid(Target, i, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "+", "/",
"-"
Results = Results & Mid(Target, i, 1)
End Select
Next i

GetResults = Evaluate(Results)
End Function


"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.
--
CG

  #4   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default extract text from string, leaving numbers and operators

One more glitch. What if there are decimals in the numbers? As the formula
is now, .5 becomes 5...not good.
--
CG


"CEG" wrote:

This works well except when there is a result entered (i.e. 4 * 40 = 160).
If some goober enters 4 * 40 = 140, I want to be able to spot it without my
calculator.

Thanks for your help so far!!!
--
CG


"Joel" wrote:

Try this UDF

Function GetResults(Target As String)

Results = ""
For i = 1 To Len(Target)
Select Case Mid(Target, i, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "+", "/",
"-"
Results = Results & Mid(Target, i, 1)
End Select
Next i

GetResults = Evaluate(Results)
End Function


"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.
--
CG

  #5   Report Post  
Posted to microsoft.public.excel.misc
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


  #6   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default extract text from string, leaving numbers and operators

Thanks Ron. This does ignore what comes after the =, which is great, but I
still have the problem of numbers with decimals. See my last post before
this one.
--
CG


"Ron Rosenfeld" wrote:

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

  #7   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default extract text from string, leaving numbers and operators

I figured it out...added a decimal in the Pattern list. Thanks!!!!!
--
CG


"CEG" wrote:

Thanks Ron. This does ignore what comes after the =, which is great, but I
still have the problem of numbers with decimals. See my last post before
this one.
--
CG


"Ron Rosenfeld" wrote:

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

  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default extract text from string, leaving numbers and operators

On Fri, 20 Feb 2009 13:37:01 -0800, CEG wrote:

I figured it out...added a decimal in the Pattern list. Thanks!!!!!
--
CG


Glad to help. Thanks for the feedback.

Adding a decimal to the pattern list will work so long as there are no decimals
that are part of the text to be eliminated.

If that might be the case, post some examples and I could rework the pattern.

Also, you should be aware that using the decimal within the pattern list (i.e.
[.] works; but if the decimal were to be used outside of the pattern list, it
has a different meaning (it would match any character in that circumstance).
--ron
  #9   Report Post  
Posted to microsoft.public.excel.misc
CEG CEG is offline
external usenet poster
 
Posts: 29
Default extract text from string, leaving numbers and operators

I figured a workaround for the decimal vs period problem. However, I do have
one other issue€¦is there a way to extract any 7-digit numbers? Sometimes
there is a reference number used that does not belong in the calculation.
--
CG


"Ron Rosenfeld" wrote:

On Fri, 20 Feb 2009 13:37:01 -0800, CEG wrote:

I figured it out...added a decimal in the Pattern list. Thanks!!!!!
--
CG


Glad to help. Thanks for the feedback.

Adding a decimal to the pattern list will work so long as there are no decimals
that are part of the text to be eliminated.

If that might be the case, post some examples and I could rework the pattern.

Also, you should be aware that using the decimal within the pattern list (i.e.
[.] works; but if the decimal were to be used outside of the pattern list, it
has a different meaning (it would match any character in that circumstance).
--ron

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
HOW DO I EXTRACT NUMBERS FROM TEXT STRING AndyF Excel Discussion (Misc queries) 7 August 14th 08 02:37 PM
extract numbers from a string Dave Excel Worksheet Functions 3 August 7th 08 09:43 PM
extract numbers from text string thomsonpa New Users to Excel 4 December 17th 07 11:02 AM
Only extract numbers from a string of text Lost in Microbiology Excel Discussion (Misc queries) 4 October 22nd 07 03:39 PM
EXTRACT NUMBERS FROM TEXT STRING fiber_doc Excel Worksheet Functions 4 November 28th 05 06:40 PM


All times are GMT +1. The time now is 01:50 AM.

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"