Extract strings from cells that contain ####-##-###-#### or ############# where # is a digit (0-9) & "-" is hyphen character

ellison

Active Member
Joined
Aug 1, 2012
Messages
356
Office Version
  1. 365
Platform
  1. Windows
Hi, we are trying to tidy up data where the part number has been jumbled into the rest of the text in the cell

Part numbers are denoted in 2 patterns. If # is digit [0-9] and "-" is the hyphen character:
1. ####-##-###-####
2. #############
Is there a way for excel to extract the references?
Please find a table below to give some examples of data:

lineRaw_InfoResult_1Result_2Result_3Explanation
1SC counted in 9999999999999 & 2244-99-999-999999999999999992244-99-999-99992 numeric references that match the 2 patterns we are after
2location ref 99999991 numeric reference but it doesn't match either of the patterns
31600-42-123-4567, last checked 04/05/20221600-42-123-45671 numeric reference does match
4faulty 8666-17-663-5897, try 65788654437428666-17-663-589765788654437422 numeric references that match
5nothing in here at all0 numeric references at all
6found 4240-84-955-0147, lost 7946135710000, alt 7090-55-741-96314240-84-955-014779461357100007090-55-741-96313 numeric references that match the 2 patterns we are after


Huge thanks for any help
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about
+Fluff 1.xlsm
ABCDE
1lineRaw_InfoResult_1Result_2Result_3
21SC counted in 9999999999999 & 2244-99-999-999999999999999992244-99-999-9999
32location ref 9999999 
431600-42-123-4567, last checked 04/05/20221600-42-123-4567
54faulty 8666-17-663-5897, try 65788654437428666-17-663-58976578865443742
65nothing in here at all 
76found 4240-84-955-0147, lost 7946135710000, alt 7090-55-741-96314240-84-955-014779461357100007090-55-741-9631
Main
Cell Formulas
RangeFormula
C2:D2,C7:E7,C5:D5,C3:C4,C6C2=IFERROR(TRANSPOSE(FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B2," ","</m><m>"),"&","&amp;"),",","")&"</m></k>","//m[(number() and string-length()=13) or (contains(.,'-') and string-length()=16)]")),"")
Dynamic array formulas.
 
Upvote 0
Solution
That's absolutely WONDERFUL, huge thanks.

[By the way, anybody that's using it, you may want to get rid of any brackets in your raw-info (square or normal) as they seem to disrupt the formula]

And thanks once again!!!

With Warm Regards :) :) :)
 
Upvote 0
Hi VBA Option
VBA Code:
Sub test()
    Dim i&, ii&
    With CreateObject("VBScript.RegExp")
        .Pattern = "(\d+(\-\d+)+)|(\d+){8,}"
        .Global = True
        For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            Set x = .Execute(Cells(i, 2))
            For ii = 0 To x.Count - 1:  Cells(i, ii + 3) = x(ii): Next
        Next
    End With
End Sub
 
Upvote 0
VBA option also works beautifully! We are spoilt for choice for solutions!!!! :ROFLMAO:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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