Extract Text at End of String

pto160

Active Member
Joined
Feb 1, 2009
Messages
482
Office Version
  1. 365
Platform
  1. Windows
I am on the latest version of Excel 365.
I decided to set up a new thread because this is a different question even though there is some similarities to the thread below that I posted.

I am comparing my transaction ID in my accounting system to the bank. I am trying to pull the tracking ID so I can compare them and use the filter formula.
In the accounting system there could be letters at the end, but I want to remove them in a different column. I am using the substitute formula, but I am getting the incorrect result.

On the bank statement, there is always a space at the end of the tracking number and a space before. I wrapped the text in this example.
TRACK9815789 CUSTOMER: Accounts Payable

Book1
ABC
1Accounting SystemWhat I am gettingWhat I want
2TRK9815789TRACK81578TRACK9815789
3TRK9815789ATRCK9815789TRACK9815789
4TRK9815789BTRACK9815789TRACK9815789
5TRK9815789CTRAK9815789TRACK9815789
6TRK9815789AATRCK9815789TRACK9815789
7TRK9815789AAATRACK9815789AAATRACK9815789
8
9BankWhat I want
10Account:1000, Bank:Bank of England, TRACK9815789 CUSTOMER: Accounts PayableTRACK9815789
11Account:1000, Bank:Bank of Scotland, Type:Checking TRACK9820789 Party:Henry LeeTRACK9820789
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=SUBSTITUTE(SUBSTITUTE(A2,"TRK","TRACK"),RIGHT(A2,1),"")
B7B7=SUBSTITUTE(A7,"TRK","TRACK")
 
:oops: Looking at post #8, I realise that I mis-read the requirement for the top section.
But for now I will stick with my suggestion for the second section, :)
 
Last edited:
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
A lot of nice answers around allready! (y)

---

The below will look extensive but I'll post it anyways since it will check for any substring that:

* Starts with either 'TRK' or 'TRACK';
* When starts with 'TRK', check that the string from position 4 onwards is numeric for the total length of the same substring when all uppercase alpha chars are replaced. This would ensure that there could only be trailing characters left en the number part is a continuation of digits;
* The same as the previous would happen when the substring starts with 'TRACK', but only from position 6 onwards;
* In case the union of the two patterns has more than 1 hit, choose the 1st one.

Book1
AB
1Accounting SystemWhat I want
2TRK9815789789TRK9815789789
3TRK9815789474ATRK9815789474
4TRK9815789BTRK9815789
5TRK9815789CTRK9815789
6TRK9815789AATRK9815789
7TRK981578945AAATRK981578945
8TRK1B2
9Bank
10Account:1000, Bank:Bank of England, TRACK981578923 CUSTOMER: Accounts PayableTRACK981578923
11Account:1000, Bank:Bank of Scotland, Type:Checking TRACK98207891 Party:Henry LeeTRACK98207891
12Account:1000, Bank:Bank of Scotland, TRK98207891ABC Party:Henry LeeTRK98207891
13Account:1000, Bank:Bank of Scotland, TRK98207891ABC123 or TRACK12345ABC Party:Henry LeeTRACK12345
14Account:1000, Bank:Bank of Scotland, TRK123 or TRACK123 Party:Henry LeeTRK123
Sheet1
Cell Formulas
RangeFormula
B2:B14B2=MAP(A2:A14,LAMBDA(z,LET(x,FILTERXML("<t><s>"&TEXTJOIN("</s><s>",,TEXTSPLIT(z,{",";" "}))&"</s></t>","(//s[starts-with(.,'TRK')][substring(.,4, string-length(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYA', '')))*0=0]|//s[starts-with(.,'TRACK')][substring(.,6, string-length(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYA', '')))*0=0])[1]"),IFERROR(TEXTBEFORE(x&"D",TEXTSPLIT(x&"D",{"TRK";"TRACK";0;1;2;3;4;5;6;7;8;9},,1),-1),""))))
Dynamic array formulas.
 
Last edited:
Upvote 0
Give this formula a try in cell B2...
Excel Formula:
=MAP(A2:A14,LAMBDA(x,LET(t,"TRACK"&IFNA(TEXTBEFORE(TEXTAFTER(SUBSTITUTE(UPPER(x),"TRK","TRACK"),"TRACK")&"X",CHAR(SEQUENCE(26,,65))),""),IF(LEN(t)>6,t,""))))
 
Upvote 0
You can try this if you are allowed to use macros, cuz regex gives you much more control over what you're searching.

VBA Code:
Sub REG()
        Dim Regex As Object
        Set Regex = CreateObject("vbscript.regexp")
        Dim mc As Object
        With Regex
            .Global = True
            .Pattern = "(TRACK|TRK)[0-9]+"
        
        End With
        On Error Resume Next
        
        For Each rng In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
                Set mc = Regex.Execute(rng.Value)
                rng.Offset(0, 1) = mc(0)
        Next rng

End Sub
 

Attachments

  • 1675334842909.png
    1675334842909.png
    26.8 KB · Views: 6
Upvote 0
You can try this if you are allowed to use macros, cuz regex gives you much more control over what you're searching.
Not important, but I don't see any benefit in going to the trouble of setting .Global = True for that code.
 
Upvote 0
This was the original sample data i tested on to get both the matches, though I didn't spill the second match in the corresponding cell as it wasn't asked by op.
 

Attachments

  • 1675337132948.png
    1675337132948.png
    16.4 KB · Views: 8
Upvote 0
This was the original sample data i tested on to get both the matches, though I didn't spill the second match in the corresponding cell as it wasn't asked by op.
Just a headsup, but that was not original sample data from OP. I added these edge-cases in my answer in post #12 just to showcase how my formula would work a little different to the other answers.

FYI, I explicitly wanted to avoid matching the TRK98207891ABC123 example as **no** match, hence the sample and use of xpath-expressions to assert this is true.
 
Upvote 0
Thanks so much to everyone. My head is spinning. These formulas are great. (y) :) This will make my work so much easier.

Rick- I did not think to add a condition that if it did not find TRK or TRACK leave the cell blank. Thanks for adding this. I have never heard of MAP. It seems powerful when combined with LAMDA.

PeterSS_s- I wrapped the formula in substitute, which seems to work. The sequence function is a great function. Thank you.

Excel Formula:
=substitute(LEFT(A2,LEN(A2)-LEN(TRIM(RIGHT(CONCAT(IF(ISNUMBER((0&MID(A2,SEQUENCE(LEN(A2)),1))+0),REPT(" ",50),1)),50)))),"TRK","TRACK"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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