Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Extract Number from text

I have cells with data such as
0.5 ml
1ml
560 gm
373 milliliters

and need a function to extract the number part of these. I found a
formula that sort of works,
-----------------
Function ExtractNumber(rCell As Range)
Dim iCount As Integer, i As Integer
Dim sText As String
Dim lNum As String

''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid Business Applications
'www.ozgrid.com

'Extracts a number from a cell containing text and numbers.
''''''''''''''''''''''''''''''''''''''''''
sText = rCell

For iCount = Len(sText) To 1 Step -1
If IsNumeric(Mid(sText, iCount, 1)) Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
End If

If i = 1 Then lNum = CInt(Mid(lNum, 1, 1))
Next iCount


ExtractNumber = CLng(lNum)
End Function
-----------------------
However this formula seems to ignore decimal points and for example the
0.5 is returned as 5. All I want is a function to return just number
part and not the units. Any ideas?

-Andrew V. Romero

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Extract Number from text

Function ExtractNumber(rCell As Range)
Dim iCount As Integer, i As Integer
Dim sText As String
Dim lNum As String

''''''''''''''''''''''''''''''''''''''''''
'Extracts a number from a cell containing text and numbers.
''''''''''''''''''''''''''''''''''''''''''
sText = rCell

For iCount = Len(sText) To 1 Step -1
If IsNumeric(Mid(sText, iCount, 1)) or _
Mid(sText, iCount,1) = "." Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
End If

Next iCount


ExtractNumber = CDbl(lNum)
End Function

--
Regards,
Tom Ogilvy

wrote in message
ups.com...
I have cells with data such as
0.5 ml
1ml
560 gm
373 milliliters

and need a function to extract the number part of these. I found a
formula that sort of works,
-----------------
Function ExtractNumber(rCell As Range)
Dim iCount As Integer, i As Integer
Dim sText As String
Dim lNum As String

''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid Business Applications
'www.ozgrid.com

'Extracts a number from a cell containing text and numbers.
''''''''''''''''''''''''''''''''''''''''''
sText = rCell

For iCount = Len(sText) To 1 Step -1
If IsNumeric(Mid(sText, iCount, 1)) Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
End If

If i = 1 Then lNum = CInt(Mid(lNum, 1, 1))
Next iCount


ExtractNumber = CLng(lNum)
End Function
-----------------------
However this formula seems to ignore decimal points and for example the
0.5 is returned as 5. All I want is a function to return just number
part and not the units. Any ideas?

-Andrew V. Romero



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Extract Number from text

Try this:
First set a reference to Microsoft VBScript Regular Expressions 5.5
this will strip out the first numeric value including decimal, even if it is
in the middle of the string

so
GetNumber("-56,424.45 sldkfns")
GetNumber("-56,424.45sldkfns")
GetNumber("sagsadgag -56,424.45sldkfns")

will all return the same value -56424.45

HS
-----------------------------------------

Function GetNumber(stringVal As String) As Double

Dim regEx, Match, Matches ' Create variable.
Set regEx = New REGEXP ' Create a regular expression.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Const patrn1 = "[0-9\.\,\-]+" 'look for any digit 0 to 9 or decimal
point or comma or the minus sign

regEx.Pattern = patrn1 ' Set pattern.

Set Matches = regEx.Execute(stringVal) ' Execute search.
If Matches.Count 0 Then
GetNumber = CDbl(Matches(0).Value)
Else
GetNumber = 0
End If

Set Matches = Nothing
Set regEx = Nothing

End Function






wrote in message
ups.com...
:I have cells with data such as
: 0.5 ml
: 1ml
: 560 gm
: 373 milliliters
:
: and need a function to extract the number part of these. I found a
: formula that sort of works,
: -----------------
: Function ExtractNumber(rCell As Range)
: Dim iCount As Integer, i As Integer
: Dim sText As String
: Dim lNum As String
:
: ''''''''''''''''''''''''''''''''''''''''''
: 'Written by OzGrid Business Applications
: 'www.ozgrid.com
:
: 'Extracts a number from a cell containing text and numbers.
: ''''''''''''''''''''''''''''''''''''''''''
: sText = rCell
:
: For iCount = Len(sText) To 1 Step -1
: If IsNumeric(Mid(sText, iCount, 1)) Then
: i = i + 1
: lNum = Mid(sText, iCount, 1) & lNum
: End If
:
: If i = 1 Then lNum = CInt(Mid(lNum, 1, 1))
: Next iCount
:
:
: ExtractNumber = CLng(lNum)
: End Function
: -----------------------
: However this formula seems to ignore decimal points and for example the
: 0.5 is returned as 5. All I want is a function to return just number
: part and not the units. Any ideas?
:
: -Andrew V. Romero
:


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Extract Number from text

Just modify the IsNumeric line:
If IsNumeric(Mid(sText, iCount, 1)) Or (Mid(sText, iCount, 1)=".") Then...
--
- K Dales


" wrote:

I have cells with data such as
0.5 ml
1ml
560 gm
373 milliliters

and need a function to extract the number part of these. I found a
formula that sort of works,
-----------------
Function ExtractNumber(rCell As Range)
Dim iCount As Integer, i As Integer
Dim sText As String
Dim lNum As String

''''''''''''''''''''''''''''''''''''''''''
'Written by OzGrid Business Applications
'www.ozgrid.com

'Extracts a number from a cell containing text and numbers.
''''''''''''''''''''''''''''''''''''''''''
sText = rCell

For iCount = Len(sText) To 1 Step -1
If IsNumeric(Mid(sText, iCount, 1)) Then
i = i + 1
lNum = Mid(sText, iCount, 1) & lNum
End If

If i = 1 Then lNum = CInt(Mid(lNum, 1, 1))
Next iCount


ExtractNumber = CLng(lNum)
End Function
-----------------------
However this formula seems to ignore decimal points and for example the
0.5 is returned as 5. All I want is a function to return just number
part and not the units. Any ideas?

-Andrew V. Romero


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 to extract text from number/text cell Access Joe Excel Worksheet Functions 6 December 1st 09 07:35 PM
Extract a number(s) from a text string Christopher770 Excel Discussion (Misc queries) 11 March 23rd 09 02:27 PM
Extract a number from a variable text string tipsy Excel Discussion (Misc queries) 4 May 4th 08 03:28 AM
extract number and use in formula from text & numbers in cell ivory_kitten Excel Worksheet Functions 3 July 14th 06 05:38 AM
Extract number from text/number string.. nastech Excel Discussion (Misc queries) 5 July 5th 06 11:21 PM


All times are GMT +1. The time now is 04:48 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"