Lookup Value and Replace Based on Table data

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I need a formula that will replace the countries in a field in Sheet1 by looking at Sheet2 and replacing the country with the corresponding Country Code.
Below is just a sample of the data.

Sheet1:
Country
UK
Singapore
US
All Costa Rica;Argentina;Bahamas;Brazil;Colombia;Costa Rica CSC;Mexico;Venezuela
Ireland;UK
Jersey;Luxembourg;Switzerland;UK
Canada;US
Algeria;All Costa Rica;Argentina;Australia;Brazil;Bulgaria;Cameroon;Canada;Colombia;Congo;Costa Rica;Czech;Dominican Republic;Ecuador;Egypt;Gabon;Guatemala;Honduras;Hong Kong;Hungary;India;Indonesia;Israel;Kazakhstan;Kenya;Mexico;Nigeria;Pakistan;Panama;Paraguay;Peru;Philippines;Puerto Rico;Qatar;Romania;Russia;Singapore;Slovakia;South Africa;Switzerland;Taiwan;Tanzania;Thailand;Trinidad & Tobago;Turkey;UAE;UK;Ukraine;Uruguay;US;Venezuela;Zambia
Japan

Sheet2:
CountryCountry Coded
AlgeriaAlgeria(DZ)
ArgentinaArgentina(AR)
ArubaAruba(AW)
Asia HubAsia Hub(X6)
AustraliaAustralia(AU)
AustriaAustria(AT)


Thank you,
Miriam
 

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
Are you have all country Names in one cell and want two replace them with hardcoded One b One?

Please Upload Example file & Desired Resultswith XL2BB ADDIN(Preferable) or upload at free hosting Site e.g. www.dropbox.com , GoogleDrive or OneDrive & insert Link here.
 
Upvote 0
I Suppose your Data at sheet1 is in Column A & At Sheet2 At Column A & B. This Macro Paste Result At Column B In Sheet1.
if you want replace Values. Change Cell.Offset(0, 1).Value = Val to Cell.Value = Val
VBA Code:
Sub ReplaceHardCeded()
Dim Lr1 As Long, Lr2 As Long, Val As String, Cell As Range, I As Long
Dim Sh1 As Worksheet, Sh2 As Worksheet, Arr As Variant, NStr As String

On Error Resume Next
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr1 = Sh1.Cells(Rows.Count, 1).End(xlUp).Row
Lr2 = Sh2.Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each Cell In Sh1.Range("A2:A" & Lr1)
 Arr = Split(Cell.Value, ";")

For I = 0 To UBound(Arr)
NStr = ""
NStr = Application.WorksheetFunction.VLookup(Arr(I), Sh2.Range("A2:B" & Lr2), 2, False)
If NStr = "" Then
NStr = Arr(I)
Else
Arr(I) = NStr
End If
If I > 0 Then
Val = Val & "; " & NStr
Else
Val = NStr
End If
Next I
Cell.Offset(0, 1).Value = Val
Val = ""
Next Cell
Application.ScreenUpdating = True
Application.DisplayAlerts = True
 
End Sub
 
Upvote 0
Hello,

I know it's been a while since I've looked at this post, but is there a way to update this code to be a UDF instead?


Thank you
 
Upvote 0
Try This:
VBA Code:
Public Function ReplaceHardCoded(SrRange As Range) As String
Dim Lr1 As Long, Lr2 As Long, Val As String, Cell As Range, I As Long
Dim Sh1 As Worksheet, Sh2 As Worksheet, Arr As Variant, NStr As String

On Error Resume Next
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr1 = Sh1.Cells(Rows.Count, 1).End(xlUp).Row
Lr2 = Sh2.Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Arr = Split(SrRange.Value, ";")

For I = 0 To UBound(Arr)
NStr = ""
NStr = Application.WorksheetFunction.VLookup(Arr(I), Sh2.Range("A2:B" & Lr2), 2, False)
If NStr = "" Then
NStr = Arr(I)
Else
Arr(I) = NStr
End If
If I > 0 Then
Val = Val & "; " & NStr
Else
Val = NStr
End If
Next I
ReplaceHardCoded = Val
Val = ""
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Function
 
Upvote 0
Solution
Try This:
VBA Code:
Public Function ReplaceHardCoded(SrRange As Range) As String
Dim Lr1 As Long, Lr2 As Long, Val As String, Cell As Range, I As Long
Dim Sh1 As Worksheet, Sh2 As Worksheet, Arr As Variant, NStr As String

On Error Resume Next
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Lr1 = Sh1.Cells(Rows.Count, 1).End(xlUp).Row
Lr2 = Sh2.Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Arr = Split(SrRange.Value, ";")

For I = 0 To UBound(Arr)
NStr = ""
NStr = Application.WorksheetFunction.VLookup(Arr(I), Sh2.Range("A2:B" & Lr2), 2, False)
If NStr = "" Then
NStr = Arr(I)
Else
Arr(I) = NStr
End If
If I > 0 Then
Val = Val & "; " & NStr
Else
Val = NStr
End If
Next I
ReplaceHardCoded = Val
Val = ""
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Function
How would this formula work? Would it just be
Excel Formula:
=ReplaceHardCoded(A2)
 
Upvote 0
Yes. only give it Source Cell Address.
 
Upvote 0
Are you Changed your Table for Indexing Country names & replaces Phrases at Sheet 2. Or Is it moving to another place?
 
Upvote 0
HA! I see my mistake. I changed the function name and forgot to change it here
VBA Code:
ReplaceHardCoded = Val

It works now. Thank you so much for your help.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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