Extract text from string that is specific length of characters

Country_Calc_2

New Member
Joined
Sep 16, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I need to extract a specific naming code that is always 14 characters long and always starts with 4 numbers followed by a hyphen - but could appear anywhere in the cell and does not always have a delimiter

Extraction examples are in red below, with the desired result in column B.

Please let me know if it is possible.

AB
17158|J1L-AM-JZGB|AM0010-301|2016-003858-332016-003858-33
2015-003086-28
2015-003086-28
TAK-931-2001|U1111-1192-7975|JapicCTI-163200return a blank
B9991033|2018-000124-34|333-54456M
2018-000124-34
IRB00011256|NCI-2015-00498|SOL-14124-Lreturn a blank
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this.

Insert VBA into a module.

VBA Code:
Function ExtractPattern(cell As Range) As String
    Dim regex As Object
    Dim matches As Object
    Dim match As Object

    Set regex = CreateObject("VBScript.RegExp")
    With regex
        .Global = True
        .IgnoreCase = True
        .Pattern = "\b\d{4}-\d{6}-\d{2}\b"
    End With

    Set matches = regex.Execute(cell.Value)

    If matches.Count > 0 Then
        Set match = matches(0)
        ExtractPattern = match.Value
    Else
        ExtractPattern = ""
    End If
End Function

Then insert this in cell B1 and copy it down.
Excel Formula:
=ExtractPattern(A1)
 
Upvote 0
Solution
Another way:

Book1
AB
117158|J1L-AM-JZGB|AM0010-301|2016-003858-332016-003858-33
22015-003086-282015-003086-28
3TAK-931-2001|U1111-1192-7975|JapicCTI-163200 
4B9991033|2018-000124-34|333-54456M2018-000124-34
5IRB00011256|NCI-2015-00498|SOL-14124-L 
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=IFERROR(MID(A1,SEARCH("|????-??????-??|","|"&A1&"|"),14),"")
 
Upvote 0
Thanks for the formula option as well. It is a little easier to understand to modify for similar but different situations (at least for me) than vba.
 
Upvote 0
Thanks for the formula option as well. It is a little easier to understand to modify for similar but different situations (at least for me) than vba.
The formula won't guarantee it's all numerical, only the same pattern. For example:

17158|J1KL-AM12AK-GB|AM0010-301|2016-003858-33
J1KL-AM12AK-GB
 
Upvote 0
Or use UDF
VBA Code:
Function ExtractPattern(s As String) As String
    Dim v As Variant
    For Each v In Split(s, "|")
        If v Like "####-######-##" Then
            ExtractPattern = v
            Exit For
        End If
    Next v
End Function
 
Upvote 0
Or use UDF
VBA Code:
Function ExtractPattern(s As String) As String
    Dim v As Variant
    For Each v In Split(s, "|")
        If v Like "####-######-##" Then
            ExtractPattern = v
            Exit For
        End If
    Next v
End Function
Hi Phuoc,
Did you test your code? It's not rendering as expected.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,108
Members
449,096
Latest member
provoking

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