Find and extract a substring that meets a specific character format

SuttieB2404

New Member
Joined
Sep 9, 2011
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi I'm trying to write a formula that extracts an invoice reference that is either two capital letters followed by 4 or five numbers from a variable string in a cell.
i.e extract 'AB1234' from 'Invoice 2222 Date: 01/01/2002 AB1234 Ref: A123456'

I found a post that counted double capitals that may be able to modify?
=SUM((CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-1)),1))>=64)*(CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-1)),1))<=90)
*(CODE(MID(A1,ROW(INDIRECT("2:"&LEN(A1))),1))>=64)*(CODE(MID(A1,ROW(INDIRECT("2:"&LEN(A1))),1))<=90))

Cheers
Stu
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
See if this would do it.

SuttieB2404.xlsm
AB
1Invoice 2222 Date: 01/01/2002 AB1234 Ref: A123456AB1234
2Other Text 
3 
4Last Inv BB2376BB2376
5Last Inv aB23766 
6ABC12345 or DE1234567 or FG76543FG76543
Sheet1
Cell Formulas
RangeFormula
B1:B6B1=LET(c,CODE(MID(A1,SEQUENCE(LEN(A1)),1)),s,CONCAT(3,IF(ABS(c-77.5)<13,1,IF(ABS(c-52.5)<5,2,3)),3), fr,IFERROR(FIND(31122223,s),0),fv,IFERROR(FIND(311222223,s),0),IF(fr,MID(A1,fr,6),IF(fv,MID(A1,fv,7),"")))
 
Upvote 1
Solution
See if this would do it.

SuttieB2404.xlsm
AB
1Invoice 2222 Date: 01/01/2002 AB1234 Ref: A123456AB1234
2Other Text 
3 
4Last Inv BB2376BB2376
5Last Inv aB23766 
6ABC12345 or DE1234567 or FG76543FG76543
Sheet1
Cell Formulas
RangeFormula
B1:B6B1=LET(c,CODE(MID(A1,SEQUENCE(LEN(A1)),1)),s,CONCAT(3,IF(ABS(c-77.5)<13,1,IF(ABS(c-52.5)<5,2,3)),3), fr,IFERROR(FIND(31122223,s),0),fv,IFERROR(FIND(311222223,s),0),IF(fr,MID(A1,fr,6),IF(fv,MID(A1,fv,7),"")))
Thank you, it works beautifully..
I've set some homework to understand LET and SEQUENCE as I've only recently switched from 2016 :)..
 
Upvote 0
Here is another option - as i was still working with it:
Book1
AB
1Invoice 2222 Date: 01/01/2002 AB1234 Ref: A123456AB1234
2Invoice 2222 Date: 01/01/2002 AB1234 Ref: A123457 & Cheese on ToastAB1234
3Ref: AB12345 Invoice 2222 Date: 01/01/2002 & Cheese on ToastAB12345
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=LET(ts,TEXTSPLIT(A1," "), lts,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", IFERROR(TAKE(FILTER(ts,(NOT(ISERROR(FIND(LEFT(LEFT(ts,2),1),lts)))*(NOT(ISERROR(FIND(MID(LEFT(ts,2),2,1),lts)))))),,-1),""))
 
Upvote 0
Thank you, it works beautifully..
You're welcome. Thanks for the follow-up. :)

Here is another option
I don't know what is possible with the OP's data, but that is not robust to extract the requested text format. For example:

SuttieB2404.xlsm
AB
8Invoice 2222 Date: 01/01/2002 AB1234 Ref: JD SmithJD
Sheet1
Cell Formulas
RangeFormula
B8B8=LET(ts,TEXTSPLIT(A8," "), lts,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", IFERROR(TAKE(FILTER(ts,(NOT(ISERROR(FIND(LEFT(LEFT(ts,2),1),lts)))*(NOT(ISERROR(FIND(MID(LEFT(ts,2),2,1),lts)))))),,-1),""))
 
Upvote 0
Hi @Peter_SSs, thanks for the stress test, I was working with the sample data, if that was part of the sample data, I would have added in a small check to see if the third character was numeric as below. We don't know what is possible or what is coming, the OP may want to look for 6 numbers following two capital letters at some point. I know what is stated in the OP but just playing devil’s advocate as there are any number of potentials for the data and different solutions will offer various pros and cons:
Book1
ABC
1ABC123456 or DE123456 or FG123456 FG123456
2Invoice 2222 Date: 01/01/2002 AB1234 Ref: JD SmithAB1234AB1234
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=LET(c,CODE(MID(A1,SEQUENCE(LEN(A1)),1)),s,CONCAT(3,IF(ABS(c-77.5)<13,1,IF(ABS(c-52.5)<5,2,3)),3), fr,IFERROR(FIND(31122223,s),0),fv,IFERROR(FIND(311222223,s),0),IF(fr,MID(A1,fr,6),IF(fv,MID(A1,fv,7),"")))
C1:C2C1=LET(ts,TEXTSPLIT(A1," "), lts,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", IFERROR(TAKE(FILTER(ts,(NOT(ISERROR(FIND(LEFT(LEFT(ts,2),1),lts)))*(NOT(ISERROR(FIND(MID(LEFT(ts,2),2,1),lts))))*(ISNUMBER(--MID(ts,3,1))))),,-1),""))
 
Upvote 0
just playing devil’s advocate
.. continuing that theme, this should still be AB1234 ;)

SuttieB2404.xlsm
ABC
2Invoice 2222 Date: 01/01/2002 AB1234 Ref: JD3AB1234JD3
Sheet3
Cell Formulas
RangeFormula
B2B2=LET(c,CODE(MID(A2,SEQUENCE(LEN(A2)),1)),s,CONCAT(3,IF(ABS(c-77.5)<13,1,IF(ABS(c-52.5)<5,2,3)),3), fr,IFERROR(FIND(31122223,s),0),fv,IFERROR(FIND(311222223,s),0),IF(fr,MID(A2,fr,6),IF(fv,MID(A2,fv,7),"")))
C2C2=LET(ts,TEXTSPLIT(A2," "), lts,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", IFERROR(TAKE(FILTER(ts,(NOT(ISERROR(FIND(LEFT(LEFT(ts,2),1),lts)))*(NOT(ISERROR(FIND(MID(LEFT(ts,2),2,1),lts))))*(ISNUMBER(--MID(ts,3,1))))),,-1),""))
 
Upvote 0

Forum statistics

Threads
1,217,459
Messages
6,136,766
Members
450,025
Latest member
Beginner52

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