Extract multiple 5-digit numbers from alphanumeric text

jkg77

New Member
Joined
Jun 9, 2017
Messages
5
Hello,


I need to extract multiple 5-digit numbers from a cell that contains both letters and numbers. I found a thread that solves for how to extract a single number but this doesn't work for me because my data might contain multiple 5-digit numbers.


Here is an example of how my data looks (column A). I would like to extract the 5-digit numbers to column B.


I would like to have the 5-digit numbers in the same cell, separated by commas. Also, the data might contain numbers that are more or less than 5 digits - these should be ignored.


Thank you!

Column AColumn B
1One number is 12345. And a second number is 54321.12345, 54321
2There is just 1 five digit number: 7363873638
3There is 1 five digit number (00293) but also 1 six digit number (038822), which should be ignored. Numbers might have leading zeros.00293
412345, 54321, and 55555.....but not ABC123 or 123ABC.12345, 54321, 55555

<tbody>
</tbody>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This UDF should do that:

Code:
Function Multi5(r As String) As String
Dim m
With CreateObject("vbscript.regexp")
    .Pattern = "\b\d{5}\b"
    .Global = True
    If Not .test(r) Then Exit Function
    For Each m In .Execute(r)
        Multi5 = Multi5 & m & ", "
    Next
Multi5 = Left(Multi5, Len(Multi5) - 2)
End With
End Function


Excel 2010
AB
1One number is 12345. And a second number is 54321.12345, 54321
2There is just 1 five digit number: 7363873638
3There is 1 five digit number (00293) but also 1 six digit number (038822), which should be ignored. Numbers might have leading zeros.00293
412345, 54321, and 55555.....but not ABC123 or 123ABC.12345, 54321, 55555
Sheet1
Cell Formulas
RangeFormula
B1=multi5(A1)
B2=multi5(A2)
B3=multi5(A3)
B4=multi5(A4)
 
Upvote 0
Hello,
I need to extract multiple 5-digit numbers from a cell that contains both letters and numbers. I found a thread that solves for how to extract a single number but this doesn't work for me because my data might contain multiple 5-digit numbers.
Here is an example of how my data looks (column A). I would like to extract the 5-digit numbers to column B.
I would like to have the 5-digit numbers in the same cell, separated by commas. Also, the data might contain numbers that are more or less than 5 digits - these should be ignored.
Thank you!
Column AColumn B
1One number is 12345. And a second number is 54321.12345, 54321
2There is just 1 five digit number: 7363873638
3There is 1 five digit number (00293) but also 1 six digit number (038822), which should be ignored. Numbers might have leading zeros.00293
412345, 54321, and 55555.....but not ABC123 or 123ABC.12345, 54321, 55555

<tbody>
</tbody>

Try this small modification in Pgc01's formula too (for 1 to 3 numbers):

=IFERROR(MID(A2,AGGREGATE(15,6,INDEX(ROW(INDIRECT("1:"&LEN(A2)))/(MMULT(1*(1*ISNUMBER(-MID(" "&A2,ROW(INDIRECT("1:"&LEN(A2)))+{0,1,2,3,4,5,6},1))={0,1,1,1,1,1,0}),{1;1;1;1;1;1;1})=7),),1),5),"")&
IFERROR(", "&MID(A2,AGGREGATE(15,6,INDEX(ROW(INDIRECT("1:"&LEN(A2)))/(MMULT(1*(1*ISNUMBER(-MID(" "&A2,ROW(INDIRECT("1:"&LEN(A2)))+{0,1,2,3,4,5,6},1))={0,1,1,1,1,1,0}),{1;1;1;1;1;1;1})=7),),2),5),"")&
IFERROR(", "&MID(A2,AGGREGATE(15,6,INDEX(ROW(INDIRECT("1:"&LEN(A2)))/(MMULT(1*(1*ISNUMBER(-MID(" "&A2,ROW(INDIRECT("1:"&LEN(A2)))+{0,1,2,3,4,5,6},1))={0,1,1,1,1,1,0}),{1;1;1;1;1;1;1})=7),),3),5),"")


Markmzz
 
Last edited:
Upvote 0
Thanks Scott! As an VBA newbie, I'm wondering if there's a formula solution?? (Or maybe it's easier if I just figure out how to implement your solution.)
 
Upvote 0
Thanks Markmzz - that definitely works for up to 3 numbers. I see how I can add more numbers, if needed, so this might work best for me.

Cheers!
Justin.
 
Upvote 0
Thanks Scott! As an VBA newbie, I'm wondering if there's a formula solution?? (Or maybe it's easier if I just figure out how to implement your solution.)
I know you have a rather lengthy formula solution, but if you should want to explore Scott's UDF solution, here is how you would implement it...

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 code Scott posted in Message #2 into the code window that just opened up. That's it.... you are done. You can now use Multi5 just like it was a built-in Excel function. For example,

=Multi5(A2)

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.
 
Last edited:
Upvote 0
If you want to extract more than 3 numbers with a formula, give this a try. Enter the formula in C1 with Ctrl - Shift - Enter, then copy across and down. Then enter the formula in b1 and copy down. You get the list in B1.

Excel Workbook
ABCDEFG
1One number is 12345. And a second number is 54321 and 91333 or 56784 or 0085691333, 56784, 54321, 12345, 008569133356784543211234500856
2There is just 1 five digit number: 736387363873638
3There is 1 five digit number (00293) but also 1 six digit number (038822), which should be ignored. Numbers might have leading zeros.0029300293
4h12345, 54321, and 55555.....but not ABC123 or 123ABC.55555, 54321, 12345555555432112345
5erter h1234r 123456 oerte
Sheet3
 
Upvote 0
Hi!

If István's suggestion (with help columns) is possible for you, try this too:

In C2 and copy down and to the right

=IFERROR(MID($A2,AGGREGATE(15,6,INDEX(ROW(INDIRECT("1:"&LEN($A2)))/
(MMULT(1*(1*ISNUMBER(-MID(" "&$A2,ROW(INDIRECT("1:"&LEN($A2)))+{0,1,2,3,4,5,6},1))={0,1,1,1,1,1,0}),{1;1;1;1;1;1;1})=7),),COLUMNS($C2:C2)),5),"")


In B2 and copy down

=SUBSTITUTE(TRIM(C2&" "&D2&" "&E2&" "&F2&" "&G2&" "&H2&" "&I2&" "&J2)," ",", ")


ABCDEFGHIJK
1DataResultHelpCol01HelpCol02HelpCol03HelpCol04HelpCol05HelpCol06HelpCol07HelpCol08
210/12/2010.IBN.NTL.WebcastEBrochure.54695LVWCR_V25469554695
310/12/2010.IBN.NTL.WebcastEBrochure.54695LVWCR_V25469554695
410/13/2010.IBN.NTL.WebcastEBrochure.54713LVWCR_V25471354713
54/20/2010.IBN.NTL.WebcastEBrochure.52511LVWCR_V15251152511
61/19/2010.IBN.NTL.WebcastEBrochure.52509LVWCR_V15250952509
757639ER_Drop1-110808-13355763957639
857818ER_Drop1-110803-06465781857818
99/14/2010.NTL.TeleBroc.55129T_V1_N5512955129
10One number is 12345. And a second number is 54321.12345, 543211234554321
11There is just 1 five digit number: 736387363873638
12Thereis 1 five digit number (00293) but also126345 1 six digit 123656 number (038822), which should be ignored. Numbers 12345 might 12345have 12345 leading zeros.12345 There12345 is 1 five digit 12345number (00293) but also1 six digit number (038822), which s00293, 12345, 12345, 12345, 12345, 12345, 12345, 002930029312345123451234512345123451234500293
1312345, 54321, and 55555.....but not ABC123 or 123ABC.12345, 54321, 55555123455432155555
14tetetet tetette tetetteete tetetet 123451234512345
15
****************************************************************************************************************************************************

<tbody>
</tbody>


Markmzz
 
Upvote 0
It seems my formula in post #8 does not extract the 5-digit number if it is the first substring of the string. Corrected version in cell C1:

=IFERROR(TEXT(LARGE(IF(NOT(ISNUMBER(--(MID(" "&$A1,ROW($1:$200),1))))*ISNUMBER(--(MID(" "&$A1,ROW($1:$200)+1,5)&"."))*NOT(ISNUMBER(--(MID(" "&$A1,ROW($1:$200)+6,1))))=1,--MID(" "&$A1,ROW($1:$200)+1,5),""),COLUMN()-2),"00000"),"")
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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