Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default extracting numerics from literal strings

If my cell A1 contains a string such as "Total 30 employees", how do I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default extracting numerics from literal strings

Array enter (ctlr+Shift+enter rather than just enter in B1)
=MID(A1,SMALL(IF(ISERROR(MID(A1,ROW(INDIRECT("1:"& LEN(A1))),1)*1),"",ROW(IND
IRECT("1:"&LEN(A1)))),1),LARGE(IF(ISERROR(MID(A1,R OW(INDIRECT("1:"&LEN(A1)))
,1)*1),"",ROW(INDIRECT("1:"&LEN(A1)))),1)-SMALL(IF(ISERROR(MID(A1,ROW(INDIRE
CT("1:"&LEN(A1))),1)*1),"",ROW(INDIRECT("1:"&LEN(A 1)))),1)+1)*1

is one way, but I am sure there is a better way.

--
Regards,
Tom Ogilvy


Ed wrote in message
...
If my cell A1 contains a string such as "Total 30 employees", how do I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default extracting numerics from literal strings

"Tom Ogilvy" wrote...
Array enter (ctlr+Shift+enter rather than just enter in B1)
=MID(A1,SMALL(IF(ISERROR(MID(A1,ROW(INDIRECT("1:" &LEN(A1))),1)*1),"",
ROW(INDIRECT("1:"&LEN(A1)))),1),LARGE(IF(ISERROR( MID(A1,ROW(
INDIRECT("1:"&LEN(A1))),1)*1),"",ROW(INDIRECT("1: "&LEN(A1)))),1)
-SMALL(IF(ISERROR(MID(A1,ROW(INDIRECT("1:"&LEN(A1)) ),1)*1),"",
ROW(INDIRECT("1:"&LEN(A1)))),1)+1)*1

is one way, but I am sure there is a better way.

....

Alternatives,

http://groups.google.com/groups?thre...ws.indigo. ie


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default extracting numerics from literal strings

Ed wrote:
If my cell A1 contains a string such as "Total 30 employees", how do

I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.


Hi Ed
one way:
=MID(A1,MIN(IF(ISNUMBER(VALUE(MID(A1,seq,1))),seq, 1024)),MAX(IF(ISNUMBE
R(VALUE(MID(A1,seq,1))),seq,0))-MIN(IF(ISNUMBER(VALUE(MID(A1,seq,1))),s
eq,1024))+1)
entered as array formula (CTRL+SHIFT+ENTER)
and seq as defined name with the formula: =ROW(INDIRECT("1:1024))

HTH
Frank

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default extracting numerics from literal strings

Ed

Programming method.

Sub Move_Nums()

Dim intI As Integer
Dim rngR As Range, rngRR As Range
Dim strNotNum As String, strTemp As String
Set rngRR = Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues)
For Each rngR In rngRR
strTemp = ""
For intI = 1 To Len(rngR.Value)
If Mid(rngR.Value, intI, 1) Like "[0-9]" Then
strNotNum = Mid(rngR.Value, intI, 1)
Else: strNotNum = ""
End If
strTemp = strTemp & strNotNum
Next intI
rngR.Offset(0, 1).Value = strTemp
Next rngR
End Sub

Gord Dibben Excel MVP


On Sat, 7 Feb 2004 17:07:18 -0500, "Ed" wrote:

If my cell A1 contains a string such as "Total 30 employees", how do I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default extracting numerics from literal strings

Ed

A caveat with this code.

Make sure you have an empty column to the right of your data.

Gord

On Sat, 07 Feb 2004 16:30:12 -0800, Gord Dibben <gorddibbATshawDOTca wrote:

Ed

Programming method.

Sub Move_Nums()

Dim intI As Integer
Dim rngR As Range, rngRR As Range
Dim strNotNum As String, strTemp As String
Set rngRR = Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues)
For Each rngR In rngRR
strTemp = ""
For intI = 1 To Len(rngR.Value)
If Mid(rngR.Value, intI, 1) Like "[0-9]" Then
strNotNum = Mid(rngR.Value, intI, 1)
Else: strNotNum = ""
End If
strTemp = strTemp & strNotNum
Next intI
rngR.Offset(0, 1).Value = strTemp
Next rngR
End Sub

Gord Dibben Excel MVP


On Sat, 7 Feb 2004 17:07:18 -0500, "Ed" wrote:

If my cell A1 contains a string such as "Total 30 employees", how do I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default extracting numerics from literal strings

"Gord Dibben" <gorddibbATshawDOTca wrote...
A caveat with this code.

Make sure you have an empty column to the right of your data.

....
Gord Dibben <gorddibbATshawDOTca wrote:

....
Sub Move_Nums()

....
Set rngRR = Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues)
For Each rngR In rngRR

....
rngR.Offset(0, 1).Value = strTemp

....

More of a caveat needed. Because you're offsetting from rngR rather than
rngRR or (better but still not safe) Selection, there's no guarantee that
this code would overwrite cells within Selection, possibly within rngRR. For
example, select A1:E5 with all cell containing "foo" except C3 containing
"foo123bar". The macro would happily overwrite D3 with 123 even though all
of column F may be empty. Better, perhaps, to provide udfs that would allow
the user/OP to select where to put the result.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default extracting numerics from literal strings

Couple of things about this routine:

1. It requires A1 contains one and only one numeric value.
2. Decimal points will work, but use 0.1 instead of .1

Sub test()
Dim strTemp As String

strTemp = Range("A1").Value
Do Until IsNumeric(Left(strTemp, 1)): strTemp = Mid(strTemp, 2): Loop
Do Until IsNumeric(Right(strTemp, 1)): strTemp = Mid(strTemp, 1,
Len(strTemp) - 1): Loop
Range("B1").Value = strTemp
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Ed" wrote in message
...
If my cell A1 contains a string such as "Total 30 employees", how do I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default extracting numerics from literal strings

Yet another alternative...

If you install the functions at
http://www.tmehta.com/regexp/add_code.htm, and if the string in
question is in column A, you could use the formula

=RegExpSubstitute(A1,"[^\d]*","")

The results will look like:

total 30 employees 30
total 30 emp87loyees 3087

If you have multiple numbers and want only the first set, use
=RegExpSubstitute(A3,"(^[^\d]*)(\d*)(.*$)","$2")

This will yield:

total 30 employees 30
total 30 emplo87yees 30
t12otal 30 emplo87yees 12


--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
If my cell A1 contains a string such as "Total 30 employees", how do I
extract the value (e.g. 30) from this string and put it in cell B1?

Thanks.



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default extracting numerics from literal strings

"Tushar Mehta" wrote...
Yet another alternative...

If you install the functions at
http://www.tmehta.com/regexp/add_code.htm, and if the string in
question is in column A, you could use the formula

=RegExpSubstitute(A1,"[^\d]*","")

....

While this seems to work, \D it the better/safer/more Perl-compatible way to
express the complement of the character class represented by \d. It also
does more work than it should.

=RegExpSubstitute(A1,"\D+","")

would be optimal in terms of regexp performance. [Tangent: participate in
comp.lang.perl.misc and you'll really learn how to use regular expressions.]

If you have multiple numbers and want only the first set, use
=RegExpSubstitute(A3,"(^[^\d]*)(\d*)(.*$)","$2")


Better to use

=RegExpSubstitute(A3,"^\D*(\d+).*$","$1")

and to pull the last numeral substring,

=RegExpSubstitute(A3,"^.*(\d+)\D*$","$1")

and to pull the n_th numeral substring counting left to right,

=RegExpSubstitute(A3,"^(\D*(\d+)){"&(n-1)&"}\D*(\d+).*$","$3")




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default extracting numerics from literal strings

"Harlan Grove" wrote...
....
and to pull the last numeral substring,

=RegExpSubstitute(A3,"^.*(\d+)\D*$","$1")

....

Mucked that up. Make that

=RegExpSubstitute(A3,"^.*?(\d+)\D*$","$1")


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default extracting numerics from literal strings

In article ,
says...

would be optimal in terms of regexp performance. [Tangent: participate in
comp.lang.perl.misc and you'll really learn how to use regular expressions.]

Yeah, I probably should do that. At the same time, it would be far
more useful to convince people to use regular expresssions with XL in
the first place. <g

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
"Tushar Mehta" wrote...
Yet another alternative...

If you install the functions at
http://www.tmehta.com/regexp/add_code.htm, and if the string in
question is in column A, you could use the formula

=RegExpSubstitute(A1,"[^\d]*","")

...

While this seems to work, \D it the better/safer/more Perl-compatible way to
express the complement of the character class represented by \d. It also
does more work than it should.

=RegExpSubstitute(A1,"\D+","")

would be optimal in terms of regexp performance. [Tangent: participate in
comp.lang.perl.misc and you'll really learn how to use regular expressions.]

If you have multiple numbers and want only the first set, use
=RegExpSubstitute(A3,"(^[^\d]*)(\d*)(.*$)","$2")


Better to use

=RegExpSubstitute(A3,"^\D*(\d+).*$","$1")

and to pull the last numeral substring,

=RegExpSubstitute(A3,"^.*(\d+)\D*$","$1")

and to pull the n_th numeral substring counting left to right,

=RegExpSubstitute(A3,"^(\D*(\d+)){"&(n-1)&"}\D*(\d+).*$","$3")



  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default extracting numerics from literal strings

"Tushar Mehta" wrote...
...
Yeah, I probably should do that. At the same time, it would be far
more useful to convince people to use regular expresssions with XL in
the first place. <g

...

Begging the question whether it'd be useful to try to convince Microsoft to add
regular expressions to Excel or at least VBA since they already exist in
VBScript. But we all know Microsoft's willingness to add *useful* features to
Excel. How much you wanna bet we'll get a flashing text format before regexps?

--
To top-post is human, to bottom-post and snip is sublime.
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
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
Extracting specific data from Date and time strings John Norfolk Excel Discussion (Misc queries) 3 September 24th 08 09:52 AM
Extracting numbers from alphanumeric strings Poonam Excel Worksheet Functions 5 April 5th 08 01:27 AM
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
2 more questions about extracting numbers from text strings andy from maine Excel Discussion (Misc queries) 0 March 28th 05 09:47 PM


All times are GMT +1. The time now is 10:49 AM.

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

About Us

"It's about Microsoft Excel"