Is there an Excel formula that can return the country name from the telephone number?
This video shows you how. To make your life easier, add this worksheet with five named ranges already in it:
This video shows you how. To make your life easier, add this worksheet with five named ranges already in it:
Transcript of the video:
Well, I love a good challenge. Get the country name from the phone number.
So, hey, I got tagged on LinkedIn. Someone named Y.S.
Had this Excel challenge, had this list of phone numbers, so they all start with a plus sign.
And they identify the country code and the country and said. "Any formula that could use a lookup table to get the country out of the phone number," And tagged a few people that he knew in Excel.
And thanks to Daniel who tagged me and said, "Let's see if Bill can help. He loves these challenges and questions.
Maybe you'll make a video". And yes, here it is, the video.
All right, so the first thing, I took the data that Yitzie posted and I said, "Is this going to be easy?
Can we just subtract the phone number from the end?" In the United States, phone numbers are all 10 digits, right?
But I did the math, so figure out the length, take out the length without the plus sign, and then the country length I know it's 44, 41, 350, so two, two, three, and US is just one.
And that means that the phone number length is not consistent, right?
We can't just assume it's 10 digits and then take everything off the end because some countries use nine or eight. Okay.
So then I realized we're going to have to do all countries.
I found this scrape thing on the web, scraped it, brought it into Excel.
There were some weird things like dashes. I got rid of the dashes.
And then the thing that completely I was not expecting, but I'm glad I checked home, conditional formatting, highlight cells, duplicate values.
Can you believe this?
There are places that all have the same country code.
So Australia, Christmas Island, Cocos Island, New Zealand, and is that Pitcairn? Sorry, I don't know how to pronounce it.
Oh, shoot. US and Canada, Russia and Kazakhstan, and more.
All right? So a few of these.
I just cheated and said, all right, if you have 47, then it's either Norway or these.
If it's 61, it's probably Australia, but it might be Christmas Island or Cocos Island.
But US and Canada, I had to split those out.
So for me, I went and found the 50 country codes for Canada and said, if it starts with one followed by these three digits, then it's Canada.
And I probably should go do the same thing for Russia or Kazakhstan, but in this case, I didn't.
All right?
So I ended up with a really long list of every possible country code and the country that it belongs to.
And then I realized that because we don't know how many digits there's going to be from the left, we have to organize this by the number of digits in the country code.
So I took all the six-digit country codes and the country, and I named that.
Now, my idea was to name this CC6.
It turns out you can't name it CC6 because that's a cell darn it. So I called it SIX, S-I-X.
And then here, all the four digit country codes, I called Four, F-O-U-R.
And then here, all the three digit country codes, I called Three, and then Two.
And then Russia, United States, Kazakhstan are the one-digit codes.
All right? So I have those five named ranges.
Now, to get the country code, what we're going to do is pretty easy at first, we're just going to do a VLOOKUP of the first six digits after the plus sign into here.
All right?
If it happens to be 441481, we know it is Guernsey. We're done.
All right?
But the odds are it's not going to be one of these.
And so then if that returns an NA, so if it is NA, then we need to come and look at the first four digits after the plus sign and see if it's in this table. If it is, we're done.
Otherwise, the first three digits after the plus sign in this table, then the two digits after the plus sign in this table, and then the one digit after the plus sign in this table.
All right.
This formula's not bad, and I like this formula.
I know you could do this with LET, but it's just really simple to me and anyone who can do a VLOOKUP or index and match in Excel is going to understand this. All right?
So here's the VLOOKUP of the phone number, starting in position two, because of the plus sign. We want six digits.
That's going to return text of course, so I add zero to coerce it back into a number.
Look at our VLOOKUP table called SIX, get the second item, and zero for false for an exact match. All right.
But see, the IFNA has two components, the VLOOKUP, and if that VLOOKUP returns an NA, then we're going to move on and grab the four digits after the plus sign and look in the table called Four.
And if that's an NA, then we're going to move on and grab the three digits after the plus sign, and then the two digits, and then one.
And finally, if none of those work, then we're just going to admit defeat and call it an error.
Okay.
So simple little formula, but of course, you can't just take this formula and put it into your sheet because you don't have these five named ranges.
But here I think is what's going to be the really easy way to do this.
Down in the YouTube description, I'm going to put this link.
We're going to come out to Chrome and paste that link, and you should end up at this workbook.
And then what we can do is file, save as, download a copy.
And there we are. So there's our CountryCodeLookup.
Open that. Okay.
So now, here, you are on your computer.
You're going to enable editing, and what we want to do is get these five named ranges over to your workbook.
So right here, I have any list of phone numbers, and you have sheets in your workbook, whatever they are. We're going to come to CountryCodeLookups.
I'm going to right-click on Sheet One, move or copy, create a copy to any list of phone numbers, move to end, and that should successfully create a list of range names.
There they are. So those names range are all there.
So now, we should just be able to take this cell and copy it to your first phone number, double-click to copy that down.
And I think you're going to have a pretty reliable formula that will take these numbers and pull out the country for those.
Okay, wait, let's make it safer.
So Yitzie's numbers all started with a plus sign in this formula will work for that.
If you have phone numbers that don't start with a plus sign, they're either numbers or text, then this alternate formula here is better.
It starts at the first position instead of the second position.
And if it's a mix of both plus signs and not plus signs, then how to use the LET function.
Sorry, that's only going to work in newer versions of Excel to substitute the plus sign with nothing. All right?
So you got basically 1, 2, 3 different choices of what you're going to copy to your workbook.
Also, added instructions here on copy this worksheet to any workbook with a list of phone numbers and right-click, move or copy.
And I changed it from Sheet One to CountryCodeLists, so it won't run into that same name as yours.
All right.
Well, hey, I want to thank Daniel for tagging me in that question, and I want to thank you for stopping by.
We'll see you next time for another netcast from MrExcel.
So, hey, I got tagged on LinkedIn. Someone named Y.S.
Had this Excel challenge, had this list of phone numbers, so they all start with a plus sign.
And they identify the country code and the country and said. "Any formula that could use a lookup table to get the country out of the phone number," And tagged a few people that he knew in Excel.
And thanks to Daniel who tagged me and said, "Let's see if Bill can help. He loves these challenges and questions.
Maybe you'll make a video". And yes, here it is, the video.
All right, so the first thing, I took the data that Yitzie posted and I said, "Is this going to be easy?
Can we just subtract the phone number from the end?" In the United States, phone numbers are all 10 digits, right?
But I did the math, so figure out the length, take out the length without the plus sign, and then the country length I know it's 44, 41, 350, so two, two, three, and US is just one.
And that means that the phone number length is not consistent, right?
We can't just assume it's 10 digits and then take everything off the end because some countries use nine or eight. Okay.
So then I realized we're going to have to do all countries.
I found this scrape thing on the web, scraped it, brought it into Excel.
There were some weird things like dashes. I got rid of the dashes.
And then the thing that completely I was not expecting, but I'm glad I checked home, conditional formatting, highlight cells, duplicate values.
Can you believe this?
There are places that all have the same country code.
So Australia, Christmas Island, Cocos Island, New Zealand, and is that Pitcairn? Sorry, I don't know how to pronounce it.
Oh, shoot. US and Canada, Russia and Kazakhstan, and more.
All right? So a few of these.
I just cheated and said, all right, if you have 47, then it's either Norway or these.
If it's 61, it's probably Australia, but it might be Christmas Island or Cocos Island.
But US and Canada, I had to split those out.
So for me, I went and found the 50 country codes for Canada and said, if it starts with one followed by these three digits, then it's Canada.
And I probably should go do the same thing for Russia or Kazakhstan, but in this case, I didn't.
All right?
So I ended up with a really long list of every possible country code and the country that it belongs to.
And then I realized that because we don't know how many digits there's going to be from the left, we have to organize this by the number of digits in the country code.
So I took all the six-digit country codes and the country, and I named that.
Now, my idea was to name this CC6.
It turns out you can't name it CC6 because that's a cell darn it. So I called it SIX, S-I-X.
And then here, all the four digit country codes, I called Four, F-O-U-R.
And then here, all the three digit country codes, I called Three, and then Two.
And then Russia, United States, Kazakhstan are the one-digit codes.
All right? So I have those five named ranges.
Now, to get the country code, what we're going to do is pretty easy at first, we're just going to do a VLOOKUP of the first six digits after the plus sign into here.
All right?
If it happens to be 441481, we know it is Guernsey. We're done.
All right?
But the odds are it's not going to be one of these.
And so then if that returns an NA, so if it is NA, then we need to come and look at the first four digits after the plus sign and see if it's in this table. If it is, we're done.
Otherwise, the first three digits after the plus sign in this table, then the two digits after the plus sign in this table, and then the one digit after the plus sign in this table.
All right.
This formula's not bad, and I like this formula.
I know you could do this with LET, but it's just really simple to me and anyone who can do a VLOOKUP or index and match in Excel is going to understand this. All right?
So here's the VLOOKUP of the phone number, starting in position two, because of the plus sign. We want six digits.
That's going to return text of course, so I add zero to coerce it back into a number.
Look at our VLOOKUP table called SIX, get the second item, and zero for false for an exact match. All right.
But see, the IFNA has two components, the VLOOKUP, and if that VLOOKUP returns an NA, then we're going to move on and grab the four digits after the plus sign and look in the table called Four.
And if that's an NA, then we're going to move on and grab the three digits after the plus sign, and then the two digits, and then one.
And finally, if none of those work, then we're just going to admit defeat and call it an error.
Okay.
So simple little formula, but of course, you can't just take this formula and put it into your sheet because you don't have these five named ranges.
But here I think is what's going to be the really easy way to do this.
Down in the YouTube description, I'm going to put this link.
We're going to come out to Chrome and paste that link, and you should end up at this workbook.
And then what we can do is file, save as, download a copy.
And there we are. So there's our CountryCodeLookup.
Open that. Okay.
So now, here, you are on your computer.
You're going to enable editing, and what we want to do is get these five named ranges over to your workbook.
So right here, I have any list of phone numbers, and you have sheets in your workbook, whatever they are. We're going to come to CountryCodeLookups.
I'm going to right-click on Sheet One, move or copy, create a copy to any list of phone numbers, move to end, and that should successfully create a list of range names.
There they are. So those names range are all there.
So now, we should just be able to take this cell and copy it to your first phone number, double-click to copy that down.
And I think you're going to have a pretty reliable formula that will take these numbers and pull out the country for those.
Okay, wait, let's make it safer.
So Yitzie's numbers all started with a plus sign in this formula will work for that.
If you have phone numbers that don't start with a plus sign, they're either numbers or text, then this alternate formula here is better.
It starts at the first position instead of the second position.
And if it's a mix of both plus signs and not plus signs, then how to use the LET function.
Sorry, that's only going to work in newer versions of Excel to substitute the plus sign with nothing. All right?
So you got basically 1, 2, 3 different choices of what you're going to copy to your workbook.
Also, added instructions here on copy this worksheet to any workbook with a list of phone numbers and right-click, move or copy.
And I changed it from Sheet One to CountryCodeLists, so it won't run into that same name as yours.
All right.
Well, hey, I want to thank Daniel for tagging me in that question, and I want to thank you for stopping by.
We'll see you next time for another netcast from MrExcel.