Extract 10 digit numbers from text

CPT JPmorgan

New Member
Joined
May 28, 2008
Messages
19
There is a column in a table with user-entered text. I need to add a column that will extract any 10 digit number that may appear in that text, which may be anywhere among the text. There won't always be a 10 digit number, and thats ok. I just want to add a column that shows the number if there is one. Also, if there are more than 1, it would be NICE to add another column for each one, but not critical. (I can live with just 1)

The above is what I really NEED, but I figure I might be able to solve another potential issue if someone has a way to populate that column with some sort of customizable search like "X+10numbers" or "Y+6numbers". Like if I was able to enter P######, and it would populate the column with every occurence of a P followed by 6 numbers. Or just enter ###, and it would populate any 3 digit number. This isn't the urgent part, but I figure if someone out there already has a solution like this, than it would be even better for what I need.

Thanks in advance for any input!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'm not entirely certain if this will work for you (possibly using some standard formulas or a vba tweak), but here's a UDF.

the function takes three inputs: the value you're trying to find a number in, the length of the number you're looking for, and which instance of a possible match you want to extract.

Note: if you're looking for a 3 digit number and the cell contains "1234", both "123" and "234" are legitimate matches.

Code:
Public Function NumberExtract(StringToSearch, LengthofNbr As Long, InstanceNbr As Long) As String
    Dim i                               As Long
    Dim strLikeString                   As String
    Dim j                               As Long
    For i = 1 To LengthofNbr
        strLikeString = strLikeString & "[0-9]"
    Next i
    For i = 1 To Len(StringToSearch) - LengthofNbr + 1
        If Mid(StringToSearch, i, LengthofNbr) Like strLikeString Then
            j = j + 1
            If j = InstanceNbr Then
                NumberExtract = Mid(StringToSearch, i, LengthofNbr)
                Exit Function
            End If
        End If
    Next i
End Function
 
Upvote 0
Thanks for the reply. Sorry it took me so long to reply with the holiday weekend and all. I'm not sure how this works. I'm still experimenting with it, but how do yoou actually use the function? i know its not just =numberextract(a1), but im still trying to figure out the propert use.
 
Upvote 0
No apologies needed for the late reply: I got a three day weekend too.

You'll need to save the function into a code module: either the workbook that's being worked on or your personal workbook (which I can't give much help on because I don't use one).

Then, the function takes 3 inputs: the first input is the string to be searched, the second input is how long of a number you want to find, and the third number is which instance you want to find.

Excel Workbook
ABCDEF
1abc123xyz456ijk1234561223
Sheet5
Excel 2007
Cell Formulas
RangeFormula
B1=NumberExtract($A$1,4,1)
C1=NumberExtract($A$1,3,1)
D1=NumberExtract($A$1,3,2)
E1=NumberExtract($A$1,2,1)
F1=NumberExtract($A$1,2,2)
 
Upvote 0
Thanks. For some reason, that didnt work earlier. I figured it was something like that, but I wasnt getting any results. I just tried again and it worked perfectly. I think excel was just mad at me for trying to get all my weekly reports done and caught up at the same time, lol. Thanks again!
 
Upvote 0
The above is what I really NEED, but I figure I might be able to solve another potential issue if someone has a way to populate that column with some sort of customizable search like "X+10numbers" or "Y+6numbers". Like if I was able to enter P######, and it would populate the column with every occurence of a P followed by 6 numbers. Or just enter ###, and it would populate any 3 digit number.
I think this function (which can be used as a UDF) will do what you asked for above...

Code:
Function GetPattern(Source As String, ByVal Pattern As String) As String
  Dim X As Long, FindPattern As Long
  Do Until Left(Pattern, 1) <> "*"
    Pattern = Mid(Pattern, 2)
  Loop
  For X = 1 To Len(Source)
    If Mid(Source, X) Like Pattern & "*" Then
      FindPattern = X
      Exit For
    End If
  Next
  For X = 1 To Len(Source) - FindPattern + 1
    If Mid(Source, FindPattern, X) Like Pattern Then
      GetPattern = Mid(Source, FindPattern, X)
      Exit For
    End If
  Next
End Function

The pattern formation rules are the same as for the Visual Basic "Like Operator". Note that the special characters can be mixed with regular characters as needed. So, you can use the patterns you indicated in your posting "P######", "###", etc. You can also make more complex patterns, for example, "P##[A-Za-z]#X" which would return a text string starting with a "P", followed by two digits, followed by any upper or lower case letter, followed by one more digit and ending with an "X". Here is the description of the pattern matching rules from the VB help files...

<TABLE dir=ltr border=1 cellSpacing=1 cellPadding=6 width=623><TBODY><TR><TD vAlign=center width="18%">
Characters in pattern





</TD><TD vAlign=center width="82%">
Matches in string





</TD></TR><TR><TD vAlign=center width="18%">?

</TD><TD vAlign=center width="82%">Any single character.

</TD></TR><TR><TD vAlign=center width="18%">*

</TD><TD vAlign=center width="82%">Zero or more characters.

</TD></TR><TR><TD vAlign=center width="18%">#

</TD><TD vAlign=center width="82%">Any single digit (0–9).

</TD></TR><TR><TD vAlign=center width="18%">[charlist]

</TD><TD vAlign=center width="82%">Any single character in charlist.

</TD></TR><TR><TD vAlign=center width="18%">[!charlist]

</TD><TD vAlign=center width="82%">Any single character not in charlist.

</TD></TR></TBODY></TABLE>
A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in string and can include almost any character code, including digits.

Note To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in brackets. The right bracket (]) can't be used within a group to match itself, but it can be used outside a group as an individual character.

By using a hyphen () to separate the upper and lower bounds of the range, charlist can specify a range of characters. For example, [A-Z] results in a match if the corresponding character position in string contains any uppercase letters in the range A–Z. Multiple ranges are included within the brackets without delimiters.

Other important rules for pattern matching include the following:

An exclamation point (!) at the beginning of charlist means that a match is made if any character except the characters in charlist is found in string. When used outside brackets, the exclamation point matches itself.

A hyphen () can appear either at the beginning (after an exclamation point if one is used) or at the end of charlist to match itself. In any other location, the hyphen is used to identify a range of characters.

When a range of characters is specified, they must appear in ascending sort order (from lowest to highest). [A-Z] is a valid pattern, but [Z-A] is not.

The character sequence [] is considered a zero-length string ("").

The meaning of a specified range depends on the character ordering valid at run time (as determined by Option Compare and the locale setting of the system the code is running on). Using the Option Compare Binary example, the range [A–E] matches A, B and E. With Option Compare Text, [A–E] matches A, a, À, à, B, b, E, e. The range does not match Ê or ê because accented characters fall after unaccented characters in the sort order.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top