Reputation: 305
I have data in a Google spreadsheet that I need to match to their standard country codes. For e.g. Greater Chicago Area
would be US
. For all cells that need to be marked US, they seem to end with the word Area
. For all other countries, they end with their country names.
I tried the following formula but it does not seem to work -
=IFS(ISNUMBER(FIND("Area",A64)), "US", ISNUMBER(FIND("*United Kingdom",A64)), "UK")
.
Is there a way to match partial text at the end of the string?
Upvotes: 5
Views: 12246
Reputation: 10573
You can also try this single formula
=INDEX(IFERROR(REGEXREPLACE(
REGEXREPLACE(A2:A,"Canada Area","Canada"),
" Area$|, TN$",", US")))
How does the formula work?
The inner REGEXREPLACE
finds the expression Canada Area
within the text and replaces it with just Canada
.
Then, the outer REGEXREPLACE
finds where the very last Area
or TN
string and replaces them with , US
(we can further enrich the formula with more conditions if necessary)
Finally the combination of INDEX
and IFERROR
expands our formula to the whole range excluding errors (which are now returned as empty cells).
As for the regex symbols used, $
defines the end of a line and |
the OR
statement.
Functions used:
Upvotes: 0
Reputation: 121
Here is the answer if you just wanted it by name, I didn't use country codes but you can easily just do a find replace search OR define a vlookup from another range.
Answer Spreadsheet with Formulas Listed in Column Header - https://docs.google.com/spreadsheets/d/1KOYWwqY_T8aG0USy5POVLjAdl99N2PrwWs5mpQAruRg/edit?usp=sharing
Steps:
=right(A2,4)="Area"
// Let's us know almost all cases that are US =find(",",A2)
// Let's us find last word in array =LEN(A2)-C2
// Tells how many letters are to the right of the comma (the last word + one space) =D2-1
// You can use Trim()
for non standardized data but for your use case -1 works fine =right(A2,E2)
// The last word =if(B2,"United States of America",F2)
// If it ends in area set to US otherwise set it to last word in string =if(ISERR(F2),G2,if(G2="TN","United States",if(G2="TN","United States of America",if(G2="Zhejiang, China","China",if(F2="Canada Area","Canada",G2)))))
// Your special cases that need to be overridden. =vlookup(H2,'Country Codes'!A:B,2,false)
/// Will find the country code in column S of searched range based on KEY provided. If you need to clean this up for a presentation you can just replace ALL cell references with the formula in that cell until you nest all the way back to just column A.
Upvotes: 6
Reputation: 57
Your formula seems to be okay. Please try and place this FIND condition at the last, after checking for every other country code.
ISNUMBER(FIND("Area",A64)), "US"
Upvotes: 0