I've attached a file that does this for you.
I've included comments in blue on the Destinations sheet to indicate the major steps I've taken, essentially to copy the data into adjacent columns (so that the original data remains in the same order), sort the copied data by Dial_code, introduce formulae to link back to the original data (so that if you need to change prices in future and find it easier to do so if the destinations are alphabetical, then you can still do so), and then introduce named ranges for codes of equal length (L_1, L_2 etc).
In the Calls_made sheet you have a list of dialled numbers in column A, together with formulae in B2, C2 and E2 which are to be copied down at least as far as your data, but it doesn't matter if you copy too far as the hyphens indicate.
This is the main formula in E2:
=IFERROR(MATCH(1*LEFT(A2,MAX(IF(ISERROR(MATCH(1*LEFT(A2,1),L_1,0)),0,1),IF(ISERROR(MATCH(1*LEFT(A2,2),L_2,0)),0,2),IF(ISERROR(MATCH(1*LEFT(A2,3),L_3,0)),0,3),IF(ISERROR(MATCH(1*LEFT(A2,4),L_4,0)),0,4),IF(ISERROR(MATCH(1*LEFT(A2,5),L_5,0)),0,5),IF(ISERROR(MATCH(1*LEFT(A2,6),L_6,0)),0,6),IF(ISERROR(MATCH(1*LEFT(A2,7),L_7,0)),0,7),IF(ISERROR(MATCH(1*LEFT(A2,8),L_8,0)),0,8),IF(ISERROR(MATCH(1*LEFT(A2,9),L_9,0)),0,9),IF(ISERROR(MATCH(1*LEFT(A2,10),L_10,0)),0,10),IF(ISERROR(MATCH(1*LEFT(A2,11),L_11,0)),0,11))),Dial_Code,0),"-")
I've left it like this rather than shorten it as an array formula so that you can follow more easily what it does. Essentially, it takes the first digit of the dialled number and sees if there is a match with the range L_1, then the first 2 digits and compares it with L_2, then the first 3 digits with L_3, and so on. The MAX function ensures that the largest number is chosen, i.e. it matches with the largest number of digits possible, and returns the overall row where that match occurred. The formula in B2, i.e.:
and in C2:
then bring the data from the corresponding appropriate columns if there was a match, or returns a hyphen if not.
In future, all you need to do is paste new dialled digits to column A, and ensure that the formulae are copied down.
Hope this helps.
Pete
Bookmarks