Replace multiple text within a single cell

Country_Calc

New Member
Joined
Feb 14, 2017
Messages
48
Office Version
  1. 365
I need to change 2 letter country codes to the full country name, but have many countries within a single cell.

Table below shows the desired output. But it could have 30 country codes in a single cell.

AB
AD, AOAndorra, Angola
AO, AIAngola, Aguilla


It would be nice to use a formula to reference a table that has all of these, but I could also do manually if needed. Example of list is below.

2 letter code​
Country​
AD​
Andorra​
AO​
Angola​
AI​
Anguilla​
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
I used Power Query to achieve your expected results.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Column1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column1"),
    #"Split Column by Delimiter1" = Table.ExpandListColumn(Table.TransformColumns(#"Split Column by Delimiter", {{"Column2", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column2"),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter1",{{"Column1", Text.Trim, type text}, {"Column2", Text.Trim, type text}}),
    #"Removed Duplicates" = Table.Distinct(#"Trimmed Text")
in
    #"Removed Duplicates"

 
Upvote 0
Here's an alternative, "=CountryLookUp(A2)", you just have to separate the two letter codes with a comma followed by a space (", ") as the delimiter.


CodesCountry
AF, DZAfghanistan, Algeria
AI, BH, BYAnguilla, Bahrain, Belarus
AS, BV, CNAmerican Samoa, Bouvet Island, China


Alpha-2 codeCountry
AFAfghanistan
ALAlbania
DZAlgeria
ASAmerican Samoa
ADAndorra
AOAngola
AIAnguilla
AQAntarctica
AGAntigua and Barbuda
ARArgentina
AMArmenia
AWAruba
AUAustralia
ATAustria
AZAzerbaijan
BSBahamas (the)
BHBahrain
BDBangladesh
BBBarbados
BYBelarus
BEBelgium
BZBelize
BJBenin
BMBermuda
BTBhutan
BOBolivia (Plurinational State of)
BQBonaire, Sint Eustatius and Saba
BABosnia and Herzegovina
BWBotswana
BVBouvet Island
BRBrazil
IOBritish Indian Ocean Territory (the)
BNBrunei Darussalam
BGBulgaria
BFBurkina Faso
BIBurundi
CVCabo Verde
KHCambodia
CMCameroon
CACanada
KYCayman Islands (the)
CFCentral African Republic (the)
TDChad
CLChile
CNChina
CXChristmas Island
CCCocos (Keeling) Islands (the)
COColombia
KMComoros (the)
CDCongo (the Democratic Republic of the)
CGCongo (the)
CKCook Islands (the)
CRCosta Rica
HRCroatia
CUCuba
CWCuraçao
CYCyprus
CZCzechia
CICôte d'Ivoire
DKDenmark
DJDjibouti
DMDominica
DODominican Republic (the)
ECEcuador
EGEgypt
SVEl Salvador
GQEquatorial Guinea
EREritrea
EEEstonia
SZEswatini
ETEthiopia
FKFalkland Islands (the) [Malvinas]
FOFaroe Islands (the)
FJFiji
FIFinland
FRFrance
GFFrench Guiana
PFFrench Polynesia
TFFrench Southern Territories (the)
GAGabon
GMGambia (the)
GEGeorgia
DEGermany
GHGhana
GIGibraltar
GRGreece
GLGreenland
GDGrenada
GPGuadeloupe
GUGuam
GTGuatemala
GGGuernsey
GNGuinea
GWGuinea-Bissau
GYGuyana
HTHaiti
HMHeard Island and McDonald Islands
VAHoly See (the)
HNHonduras
HKHong Kong
HUHungary
ISIceland
INIndia
IDIndonesia
IRIran (Islamic Republic of)
IQIraq
IEIreland
IMIsle of Man
ILIsrael
ITItaly
JMJamaica
JPJapan
JEJersey
JOJordan
KZKazakhstan
KEKenya
KIKiribati
KPKorea (the Democratic People's Republic of)
KRKorea (the Republic of)
KWKuwait
KGKyrgyzstan
LALao People's Democratic Republic (the)
LVLatvia
LBLebanon
LSLesotho
LRLiberia
LYLibya
LILiechtenstein
LTLithuania
LULuxembourg
MOMacao
MGMadagascar
MWMalawi
MYMalaysia
MVMaldives
MLMali
MTMalta
MHMarshall Islands (the)
MQMartinique
MRMauritania
MUMauritius
YTMayotte
MXMexico
FMMicronesia (Federated States of)
MDMoldova (the Republic of)
MCMonaco
MNMongolia
MEMontenegro
MSMontserrat
MAMorocco
MZMozambique
MMMyanmar
NANamibia
NRNauru
NPNepal
NLNetherlands (the)
NCNew Caledonia
NZNew Zealand
NINicaragua
NENiger (the)
NGNigeria
NUNiue
NFNorfolk Island
MPNorthern Mariana Islands (the)
NONorway
OMOman
PKPakistan
PWPalau
PSPalestine, State of
PAPanama
PGPapua New Guinea
PYParaguay
PEPeru
PHPhilippines (the)
PNPitcairn
PLPoland
PTPortugal
PRPuerto Rico
QAQatar
MKRepublic of North Macedonia
RORomania
RURussian Federation (the)
RWRwanda
RERéunion
BLSaint Barthélemy
SHSaint Helena, Ascension and Tristan da Cunha
KNSaint Kitts and Nevis
LCSaint Lucia
MFSaint Martin (French part)
PMSaint Pierre and Miquelon
VCSaint Vincent and the Grenadines
WSSamoa
SMSan Marino
STSao Tome and Principe
SASaudi Arabia
SNSenegal
RSSerbia
SCSeychelles
SLSierra Leone
SGSingapore
SXSint Maarten (Dutch part)
SKSlovakia
SISlovenia
SBSolomon Islands
SOSomalia
ZASouth Africa
GSSouth Georgia and the South Sandwich Islands
SSSouth Sudan
ESSpain
LKSri Lanka
SDSudan (the)
SRSuriname
SJSvalbard and Jan Mayen
SESweden
CHSwitzerland
SYSyrian Arab Republic
TWTaiwan (Province of China)
TJTajikistan
TZTanzania, United Republic of
THThailand
TLTimor-Leste
TGTogo
TKTokelau
TOTonga
TTTrinidad and Tobago
TNTunisia
TRTurkey
TMTurkmenistan
TCTurks and Caicos Islands (the)
TVTuvalu
UGUganda
UAUkraine
AEUnited Arab Emirates (the)
GBUnited Kingdom of Great Britain and Northern Ireland (the)
UMUnited States Minor Outlying Islands (the)
USUnited States of America (the)
UYUruguay
UZUzbekistan
VUVanuatu
VEVenezuela (Bolivarian Republic of)
VNViet Nam
VGVirgin Islands (British)
VIVirgin Islands (U.S.)
WFWallis and Futuna
EHWestern Sahara
YEYemen
ZMZambia
ZWZimbabwe
AXÅland Islands



VBA Code:
Function CountryLookUp(rng As Range)
Dim rng1 As Range
Dim arr() As String, arr2() As Variant
Dim TempStr As String
Dim i As Long, j As Long


    If InStr(rng, ",") > 0 Then
        arr = Split(rng, ", ")
    Else
        arr = rng
    End If
    
    Set rng1 = Sheets("Country_List").Range("A2:B250")
    arr2 = rng1.Value
    
    For j = LBound(arr) To UBound(arr)
        For i = LBound(arr2) To UBound(arr2)
            If arr(j) = arr2(i, 1) And Len(TempStr) > 0 Then
               TempStr = TempStr & ", " & arr2(i, 2)
               GoTo Skip
            ElseIf arr(j) = arr2(i, 1) Then
               TempStr = arr2(i, 2)
               GoTo Skip
            End If
        Next i
Skip:
    Next j
            
    CountryLookUp = TempStr

End Function
 
Upvote 0
Another option
+Fluff 1.xlsm
ABCDE
1Alpha-2 codeCountry
2ADAndorraCA, AG, AUCanada, Antigua and Barbuda, Australia
3AEUnited Arab Emirates (the)BR, AL, BM, AZ, ADBrazil, Albania, Bermuda, Azerbaijan, Andorra
4AFAfghanistan
5AGAntigua and Barbuda
6AIAnguilla
7ALAlbania
8AMArmenia
9AOAngola
10AQAntarctica
11ARArgentina
12ASAmerican Samoa
13ATAustria
14AUAustralia
15AWAruba
16AXÅland Islands
17AZAzerbaijan
18BABosnia and Herzegovina
19BBBarbados
20BDBangladesh
21BEBelgium
22BFBurkina Faso
23BGBulgaria
24BHBahrain
25BIBurundi
26BJBenin
27BLSaint Barthélemy
28BMBermuda
29BNBrunei Darussalam
30BOBolivia (Plurinational State of)
31BQBonaire, Sint Eustatius and Saba
32BRBrazil
33BSBahamas (the)
34BTBhutan
35BVBouvet Island
36BWBotswana
37BYBelarus
38BZBelize
39CACanada
40CCCocos (Keeling) Islands (the)
Lists
Cell Formulas
RangeFormula
E2:E3E2=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(D2,", ","</m><m>")&"</m></k>","//m"),a,FILTER($A$2:$B$300,ISNUMBER(MATCH($A$2:$A$300,f,0))),TEXTJOIN(", ",,INDEX(SORTBY(a,MATCH(INDEX(a,,1),f,0)),,2)))
 
Upvote 0
Thanks everyone for the 3 different options. I am sure all of them worked, but Fluff's option was the one I was most comfortable with and tested. I tried to get it to work with referencing the table in Columns A and B on another worksheet with a named range and I could not get it to work.

It worked by setting up the column order exactly as shown. I just pasted as text and deleted after setting up.

Thanks again.
 
Upvote 0
Is column A & B a named range or a structured table?
 
Upvote 0
Ok how about
Cell Formulas
RangeFormula
B2:B3B2=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(A2,", ","</m><m>")&"</m></k>","//m"),a,FILTER(Countries,ISNUMBER(MATCH(INDEX(Countries,,1),f,0))),TEXTJOIN(", ",,INDEX(SORTBY(a,MATCH(INDEX(a,,1),f,0)),,2)))
Named Ranges
NameRefers ToCells
Countries=Sheet1!$A$2:$B$40B2:B3
 
Upvote 0
Solution

Forum statistics

Threads
1,215,603
Messages
6,125,771
Members
449,259
Latest member
rehanahmadawan

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