How are we supposed to treat the decimal/period/full stop in column B of the List of Flags sheet?
I think what has happened is that when you have only 2 entries in a cell like:
41,2345
Excel is treating it in your country as if the comma is a decimal point so it is one number (not two). When I open it in the UK, that number appears as 41.2345.
Anyway, I think I've taken care of it.
In the attached I've made your List of Flags into a proper Excel Table, called
Table1:
You can drag the bottom right corner of the table to fit your data if necessary.
Do not change the headers.
A Power Query query splits that table into another table at cell D3 of the same sheet:
It's called
Table1_2. It is sorted with the longest
Identifier numbers at the top. This is important because you want to find the longest matches first and I've used VLOOKUP which only returns the first match it comes across. If you change the data in
Table1 you will need to refresh the
Table1_2 table by right-clicking in it and choosing
Refresh.
On the
Flags Identification sheet I have a formula in column E:
=TEXTJOIN("¬",TRUE,IFERROR(VLOOKUP(LEFT(A2,SEQUENCE(1,LEN(A2),LEN(A2),-1)),
Table1_2,2,FALSE),""))
which is where the magic happens;
It takes the value in column A, let's say it is
509074 and it looks for the entire value in
Table1_2 and returns the Flag, it then looks for
50907, and returns that too, then looks for
5090, then
509 etc. right down to 1 character returning the Flag each time if it's found. It concatentates all the finds with a
'¬' character, so you might get for example:
Elo¬Aura¬Mastercard
which you can see in column E (I later hide this column).
In column F I have the formula:
=LEFT(E2,IFERROR(FIND("¬",E2)-1,999))
which takes only the portion of the string in column E before the first '
¬' character (the longest match).
That's it.