ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Extract numbers from text (https://www.excelbanter.com/excel-programming/359152-extract-numbers-text.html)

Therese

Extract numbers from text
 
Hi
I have a cell with a sentence in text and numbers in between. Is there a
formular so that if I write the formular in B1, it will give me all the
numbers or give me only the text og A1 ?
Thanks
--
Therese

Kletcho

Extract numbers from text
 
Here are a couple of User Defined Functions you can use.

Public Function PullText(rngTemp As Range)
Dim strTemp As String

For i = 1 To Len(rngTemp.Value)
If (Asc(Mid(rngTemp.Value, i, 1)) <= 47 Or
Asc(Mid(rngTemp.Value, i, 1)) = 58) Then
strTemp = strTemp & Mid(rngTemp.Value, i, 1)
End If
Next i
PullText = strTemp
End Function

Public Function PullNumbers(rngTemp As Range)
Dim strTemp As String

For i = 1 To Len(rngTemp.Value)
If (Asc(Mid(rngTemp.Value, i, 1)) = 48 And
Asc(Mid(rngTemp.Value, i, 1)) <= 57) Then
strTemp = strTemp & Mid(rngTemp.Value, i, 1)
End If
Next i
PullNumbers = strTemp
End Function


Therese

Extract numbers from text
 
hHi Kletcko
Thanks a lot for you help, I will see if I can use it, and ten efter that I
will go back to "general questions" where I belong. A bit too tough for me,
but thanks for your time.
--
Therese


"Kletcho" skrev:

Here are a couple of User Defined Functions you can use.

Public Function PullText(rngTemp As Range)
Dim strTemp As String

For i = 1 To Len(rngTemp.Value)
If (Asc(Mid(rngTemp.Value, i, 1)) <= 47 Or
Asc(Mid(rngTemp.Value, i, 1)) = 58) Then
strTemp = strTemp & Mid(rngTemp.Value, i, 1)
End If
Next i
PullText = strTemp
End Function

Public Function PullNumbers(rngTemp As Range)
Dim strTemp As String

For i = 1 To Len(rngTemp.Value)
If (Asc(Mid(rngTemp.Value, i, 1)) = 48 And
Asc(Mid(rngTemp.Value, i, 1)) <= 57) Then
strTemp = strTemp & Mid(rngTemp.Value, i, 1)
End If
Next i
PullNumbers = strTemp
End Function



Kletcho

Extract numbers from text
 
You would copy these into a module in your workbook (alt-F11 to get to
the visual basic editor and then Insert - Module to get a module.
Paste the code in the white area.)

Then in a cell in your workbook you would refer to the formula like any
other default excel formula:

=PullNumbers(A1)

or

=PullText(A1)


Ron Rosenfeld

Extract numbers from text
 
On Tue, 18 Apr 2006 09:24:02 -0700, Therese
wrote:

Hi
I have a cell with a sentence in text and numbers in between. Is there a
formular so that if I write the formular in B1, it will give me all the
numbers or give me only the text og A1 ?
Thanks


Another option would be to download and install Longre's free morefunc.xll
add-in from http://xcell05.free.fr/.

This can be distributed freely with your workbook.

You can then use the regular expression formulas:

=REGEX.SUBSTITUTE(A1,"\D") to extract the numbers and

=REGEX.SUBSTITUTE(A1,"\d") to extract everything that is not a number.

This will work unless your sentence has more than 255 characters. If it does,
similar solutions using VBA regular expressions can be devised.


--ron


All times are GMT +1. The time now is 10:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com