Compare Names between two files and get a address

nt_beans

New Member
Joined
Nov 10, 2015
Messages
26
Hello,
I've two excel files (A and B ). File A got Names of 1000 employees of our firm. File B is complete list and got names of 35000 employees of our firm including their location and country.
A macro should compare names between File A and B and populate Location in File A after looking up in File B

Example:
File A
NameLocation
Andrew Corey
Michael Wilson

<tbody>
</tbody>

File B

NameLocationCountry
Richard SpicerNew YorkUSA
Michael WilsonChicagoUSA
Rajesh SharmaBarcelonaSpain
Andew CoreySeattleUSA

<tbody>
</tbody>

Desire OutPut
A macro should compare names between File A and B and populate Location in File A after looking up in File B

File A
NameLocation
Andrew CoreySeattle
Michael WilsonChicago

<tbody>
</tbody>


Hope someone can assist with my issue.

thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If you want a macro try
Code:
Sub GetLocation()

   Dim Cl As Range
   Dim SrcSht As Worksheet
   Dim DestSht As Worksheet
   
   Set DestSht = Sheets("[COLOR=#ff0000]New[/COLOR]")
   Set SrcSht = Workbooks("[COLOR=#ff0000]Book1.xlsm[/COLOR]").Sheets("[COLOR=#ff0000]JCX[/COLOR]")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In SrcSht.Range("A2", SrcSht.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1).Value
      Next Cl
      For Each Cl In DestSht.Range("A2", DestSht.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
This looks in Cols A & B for the data.
Change the sheet & workbook names in red to suit.
The code needs to go in the Destination workbook & both books need to be open
 
Upvote 0
@codeliftsleep
Rather than telling people what you would or wouldn't do, why not show us what you'd do (ie provide the code).
 
Upvote 0
@codeliftsleep
Rather than telling people what you would or wouldn't do, why not show us what you'd do (ie provide the code).

Normally I do, but I am on a smartphone right now and don't have access to excel. I always test the code out before I post it to eliminate mistakes.

When I get home I'll write something up and post it
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
If you want a macro try
Code:
Sub GetLocation()

   Dim Cl As Range
   Dim SrcSht As Worksheet
   Dim DestSht As Worksheet
   
   Set DestSht = Sheets("[COLOR=#ff0000]New[/COLOR]")
   Set SrcSht = Workbooks("[COLOR=#ff0000]Book1.xlsm[/COLOR]").Sheets("[COLOR=#ff0000]JCX[/COLOR]")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In SrcSht.Range("A2", SrcSht.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1).Value
      Next Cl
      For Each Cl In DestSht.Range("A2", DestSht.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
This looks in Cols A & B for the data.
Change the sheet & workbook names in red to suit.
The code needs to go in the Destination workbook & both books need to be open

Dictionary is a great solution as well allowing key, value pairs. Was not even aware you could use dictionaries in VBA...only thing that could cause an issue if two people have the same name because you cant have a duplicate key with a dictionary.

This brings up some interesting possibilities for me with some things I am doing...

I'm getting ready to post this with a variant array but I doubt there would be much if any difference in speed versus a dictionary.

OP, how would you handle if someone had the exact same name in the company? There would need to be a way of differentiating which person you were wanting...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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