Extracting multiple values from alphanumeric text based on set criteria

chrispegg

New Member
Joined
Nov 12, 2012
Messages
15
Hello there,

I have a set of data which contains part numbers in text fields. Every part number starts "288" and has 10 digits.

Because the part number can appear in several columns (but not explicitly) I have concatenated the relevant columns into one alphanumeric column.

What I want to do is have a formula that extracts every unique part number from the text and separates it with a comma. For example:


Part number 2881234567 & P/N28898765432881234567,2889876543
ehwh28899999992889999999
2888889991 or 2888889992 or 28888899932888889991,2888889992,2888889993

<tbody>
</tbody>


I originally found =TRIM(LEFT(SUBSTITUTE(MID(12,FIND("288",A1),LEN(A1))," ",REPT(" ",100)),100)) which seemed to work, until I realised that it would stop after a space.

Is there any way around this?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Consider:

ABC
1Part number 2881234567 & P/N28898765432881234567,28898765432881234567,2889876543
2ehwh288999999928899999992889999999
32888889991 or 2888889992 or 28888899932888889991,2888889992,28888899932888889991,2888889992,2888889993

<tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
C1=getparts(A1)

<tbody>
</tbody>

<tbody>
</tbody>

Array Formulas
CellFormula
B1{=TEXTJOIN(",",TRUE,IF(ISERROR(MID("x"&A1,ROW(INDIRECT("1:"&LEN(A1)-2)),1)+0)*(MID("x"&A1,ROW(INDIRECT("1:"&LEN(A1)-2))+1,3)="288"),MID(" "&A1,ROW(INDIRECT("1:"&LEN(A1)-2))+1,10),""))}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



A couple of options. If you have Excel 365 with the TEXTJOIN function, you can use the (somewhat awkward) formula in B1. If you don't have that, you may need a UDF (User defined function).

To install a UDF, open your workbook, and press Alt-F11 to open the VBA editor. From the menu, select Insert > Module. Paste the following code into the window that opens:

Code:
Public Function GetParts(MyString As String) As String
Dim x As Variant
    
    With CreateObject("VBScript.RegExp")
        .Pattern = "288\d{7}"
        .Global = True
        For Each x In .Execute(MyString)
            GetParts = GetParts & "," & x
        Next x
    End With
    GetParts = Mid(GetParts, 2)

End Function
Close the editor by pressing Alt-Q. Now you can use the function in C1.

Hope one of these helps!
 
Last edited:
Upvote 0
Here is another UDF (user defined function) that you can consider...
Code:
Function GetParts(ByVal S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If Not Mid(S, X, 1) Like "#" Then Mid(S, X) = " "
  Next
  GetParts = Replace(Application.Trim(S), " ", ", ")
End Function

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 GetParts just like it was a built-in Excel function. For example,

=GetParts(A1)

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.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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