How to find a code within a string of text

EmmaTM

Board Regular
Joined
Jan 5, 2022
Messages
104
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a column with a string of text that can have different numbers of characters. I want to be able to extract the account number shown below in red so that I can do a look up with a list of customer account codes on another tab. I cannot use left or right as I said the numbers before and after will always be different.

Contractor Timesheet Pay~GEN000000000~M100004CA~E Pppp~79347~SSS Ltd~2023~1

Thank you
Emma
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Will the account number always be between the 4th & 5th ~
 
Upvote 0
Ok, how about
Fluff.xlsm
AB
1
2Contractor Timesheet Pay~GEN000000000~M100004CA~E Pppp~79347~SSS Ltd~2023~179347
Data
Cell Formulas
RangeFormula
B2B2=FILTERXML("<k><m>"&SUBSTITUTE(A2,"~","</m><m>")&"</m></k>","//m[5]")
 
Upvote 0
Power Query solution:

1670874899339.png


This will split the string into 8 columns, but you can just delete the 7 other columns as the next step.
 
Upvote 0
Automatrix, how do I implement your solution?
1. Have you data in a table. Go to Data --> Get data --> From table/range. This will make a query in Power Query, and open the Query editor window, your new best friend :)
2. Right click on the column with the data, choose Split column, then Split Column by Delimiter, and make it look like the image.
3. This will probably yield 8 columns, one of which contains the desired data (if indeed it is always between the 4th and 5th ~). Delete the other columns, and load the query as a new table.

I hope this works for you, otherwise let us know where it goes wrong.
 
Upvote 0
Ok, how about
Fluff.xlsm
AB
1
2Contractor Timesheet Pay~GEN000000000~M100004CA~E Pppp~79347~SSS Ltd~2023~179347
Data
Cell Formulas
RangeFormula
B2B2=FILTERXML("<k><m>"&SUBSTITUTE(A2,"~","</m><m>")&"</m></k>","//m[5]")
Hey - that worked on the majority of the items but not these even though they are within the 4th & 5th ~

Employers NIC Debit~GEN000000000~M10000PA~N Bbbbbb~79792~D Ltd~2023~1
Contractor Timesheet Pay~GEN00000000~M20000CA~S Hhhhhh~80041~T Ltd~2023~1
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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