Compare two spreadsheets and populate address/city/state/country

nt_beans

New Member
Joined
Nov 10, 2015
Messages
26
Hi All,

Need a help in capturing data from another spreadsheet. I have two files
1. File A (1200 rows)
NameExchangeCityAddress1Address2StateCountryPostal
Abe JohnCBOTNew York
Mark TwainCMESingapore

<tbody>
</tbody>

2. File B ( 300 rows)
Address1Address2CityStateCountryPostal
320 W, 42nd stNew YorkNYUS10048
140 Governers LaneMiamiFLUS40323
34, financial districtzone2SingaporeSG324A4

<tbody>
</tbody>

Output
I want to compare file A and B by "City" and populate address1/address2/State/Country/Postal in File A

NameExchangeCityAddress1Address2StateCountryPostal
Abe JohnCBOTNew York320 W, 42nd StNYUS10048
Mark TwainCMESingapore34, financial districtzone2SG324A4

<tbody>
</tbody>

Thanks in advance and please let me know if you have any further questions.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
How about
Code:
Sub GetAddress()

   Dim Cl As Range
   Dim CityWs As Worksheet
   Dim ExWs As Worksheet
   
   Set CityWs = Workbooks([COLOR=#ff0000]"FileB.xlsx[/COLOR]").Sheets("[COLOR=#ff0000]Cities[/COLOR]")
   Set ExWs = ThisWorkbook.Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In CityWs.Range("C2", CityWs.Range("C" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl
      Next Cl
      For Each Cl In ExWs.Range("C2", ExWs.Range("C" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            Cl.Offset(, 1).Resize(, 2).Value = .Item(Cl.Value).Offset(, -2).Resize(, 2).Value
            Cl.Offset(, 3).Resize(, 3).Value = .Item(Cl.Value).Offset(, 1).Resize(, 3).Value
         End If
      Next Cl
   End With
   
End Sub
This needs to go in file A, with file B open when you run it.
Change values in red to suit
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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