Extracting Multiple Numbers from a Text String

theboylow

New Member
Joined
Jul 7, 2015
Messages
16
Hi,

I have a question on extracting multiple numbers from a text string - not sure if this can be done but am looking for a forumla or a way to help me do this.

In cell a1 I have the following text:

The business can be contacted on 01112223333 or 01113334444 or if not available 01114445555


I'd like to see the 3 numbers shown in b1 or alternatively the three numbers extracted to show in three different cells e.g. b1, c1, d1 with one number per cell


Can anyone help?
 
A bit simplistic but how about

In B1 =MID(A1,FIND(0,A1),11)
In C1 =MID(A1,FIND(0,A1,FIND(B1,A1)+12),11)
In D1 =MID(A1,FIND(0,A1,FIND(C1,A1)+12),11)
 
Upvote 0
Here is a UDF (user defined function), the first will pull out all of the 11-digit numbers and put them in a single cell as a comma-delimited list. You would call it like this....

=UKPhoneNumbers(A1)

Here is the code for it...
Code:
Function UKPhoneNumbers(ByVal S As String) As String
  Dim X As Long, PhoneNumbers() As String
  S = Replace(S, " ", "")
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  PhoneNumbers = Split(Application.Trim(S))
  For X = 0 To UBound(PhoneNumbers)
    If Len(PhoneNumbers(X)) <> 11 Then PhoneNumbers(X) = ""
  Next
  UKPhoneNumber = Replace(Application.Trim(Join(PhoneNumbers)), " ", ", ")
End Function

NOTE: Any numbers listed with spaces between parts of them (like 0111 222 3333) will have their spaces removed in the output from the UDF.


This next UDF (note the difference in spelling... the above code ends in an "s", this next one does not) will place a single 11-digit number in the cell which you would specify by its second argument. You would call the UDF by placing it in the cell you want the first number to go in and then copying it across for as many cells as you think you will ever have phone numbers for at a maximum (don't worry if there are not enough phone numbers as the function will output the empty string "" if it runs out of 11-digit numbers). These formulas can then be copied down as needed...

=UKPhoneNumber($A1,COLUMNS($A:A))

Here is the code for it...
Code:
Function UKPhoneNumber(ByVal S As String, Number As Long) As String
  Dim X As Long, PhoneNumbers() As String
  S = Replace(S, " ", "")
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  PhoneNumbers = Split(Application.Trim(S))
  For X = 0 To UBound(PhoneNumbers)
    If Len(PhoneNumbers(X)) <> 11 Then PhoneNumbers(X) = ""
  Next
  PhoneNumbers = Split(Application.Trim(Join(PhoneNumbers)))
  If Number <= UBound(PhoneNumbers) + 1 Then UKPhoneNumber = PhoneNumbers(Number - 1)
End Function

NOTE: Any numbers listed with spaces between parts of them (like 0111 222 3333) will have their spaces removed in the output from the UDF.


HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use NameOfTheUDF just like it was a built-in Excel function. See examples provided with each descriptions for each UDF shown above.

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Cheers Rick - thansk very much for this, one of the other forum members has given me the solution I need but I will also try this one as well so that I use this sort or thing in the future
 
Upvote 0
Array formula**:

=MID($A1,SMALL(IF(MMULT(0+(ISNUMBER(0+MID(MID($A1,ROW(INDEX(A:A,1):INDEX(A:A,LEN($A1)-10)),11),{1,2,3,4,5,6,7,8,9,10,11},1))),{1;1;1;1;1;1;1;1;1;1;1})=11,ROW(INDEX(A:A,1):INDEX(A:A,LEN($A1)-10))),COLUMNS($A:A)),11)

Copy to the right as required. Add an IFERROR statement to mask errors if desired.

Note that I didn't write anything in to this set-up to account for strings which also contain numbers with more than 11 digits. Any such numbers will cause this set-up to give incorrect results (although I can modify the formula with a few tweaks if such cases are possible - let me know).

Regards


**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

I now have the solution I need but many thanks for your help on this today
 
Last edited:
Upvote 0
A bit simplistic but how about

In B1 =MID(A1,FIND(0,A1),11)
In C1 =MID(A1,FIND(0,A1,FIND(B1,A1)+12),11)
In D1 =MID(A1,FIND(0,A1,FIND(C1,A1)+12),11)

That is absolutely perfect Jim - much appreciated
Could you ever have a date in your text? If so, then JimM's formulas will give you an incorrect result for something like this...

After 01/01/2016, you can contact the business at 01112223333 or 09998887777.
 
Upvote 0
Not sure if I'm doing something wrong but if I copy this formula into cell b1 and apply it to my text in cell a1 the only thing is returned is "the busines" which is the first 11 characters of my example text - shown again below for reference

The business can be contacted on 01112223333 or 01113334444 or if not available 01114445555

Looks like you didn't follow the instructions re how to enter an array formula.

Rick's example with dates is the reason I posted such a complex-looking solution, since it avoids such issues.

Regards
 
Upvote 0
Rick's example with dates is the reason I posted such a complex-looking solution, since it avoids such issues.
At the risk of making your formula even more complex-looking, I would suggest encasing it in an IFERROR function call to output the empty string ("") instead of a #NUM! error when the text does not contain three phone numbers in it.
 
Upvote 0
At the risk of making your formula even more complex-looking, I would suggest encasing it in an IFERROR function call to output the empty string ("") instead of a #NUM! error when the text does not contain three phone numbers in it.

Agreed.

Regards
 
Upvote 0

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