ReExtract a sequence of texts and letters from a text string

Jonny99333

New Member
Joined
Dec 6, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi

I'm looking for a formula that will allow me to extract a specific sequence of numbers and letters within a long list of text strings which may appear anywhere within the text string - e.g.:

I need to find a text sequence LNNNN whereby L represents a letter and N represents a number

Heres a few examples of text strings whereby i would need a formula to extract this sequence. This text sequence could appear anywhere within the text string but will always appear after the character "-" but this character may appear numerous times within the text string:

Text Strings:

Cell A1: 564653-65341-EXPENSE-management35-G1234-4123
Cell A2: Account-R4321-SOFT4-cded-e23cxds
Cell A3: Company X example-46532testing-GRSFV-rwtgvwarsvw-123;juknbe23e-D4353-gsdbgv
Cell A4: Digital-645-A_22G534-Q4587

Results for required Formula from these text strings:


Result Cell A1: G1234
Result Cell A2: R4321
Result Cell A3: D4353
Result Cell A4: Q4587

1670362192413.png


Many thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try a UDF (User-Defined Function)

VBA Code:
Function Extract_Sequence(s As String) As String
  Dim texts As Variant
  Dim r As String
  
  s = "-" & s
  With CreateObject("VBScript.RegExp")
    .Pattern = "\-[a-zA-Z]\d\d\d\d"
    .Global = True
    If .test(s) Then
      Set texts = .Execute(s)
      Extract_Sequence = Mid(texts(0), 2)
    End If
  End With
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 Extract_Sequence just like it was a built-in Excel function. For example:

Dante Amor
AB
1
2564653-654321-EXPS-MANAG35-b1234-4123b1234
3Account-R4321-SOFT4-cded-e23cxdsR4321
4digital-645-a_22G534-A4587A4587
5X9999-dataX9999
Hoja10
Cell Formulas
RangeFormula
B2:B5B2=Extract_Sequence(A2)
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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