VBA Match 2 Fields

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,376
Some how I'm making this hard.

trying to use VBA only


I have a master list of Names on sheet "Member", First name in Column C, Last name in Column D, ID number in Column A

I have a second sheet "Sheet2" with combined names in Column I. ex: Bill Gates

I need to loop through the names in Sheet 2 and get the appropriate ID number from Member Sheet.

code so far:
Code:
...
set rs = worksheets("Master")
set myRange = worksheets("Sheet2").Range("I2:I500")
for each myCell in myRange
first = split(myCell.value, " ") (0)
last= split(myCell.value, " ") (1)

id =???

...

i've tried multiple index matches and vlookups and elvautate but no luck.

Like
Id=index.rs(Range("A:A"),Match(First & Last,rs.Range("C:D",0))

but no success.

Any Ideas?

thanks,

Ross
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How about
Code:
Sub copydata()

   Dim Cl As Range
   Dim ValU As String
   Dim WsM As Worksheet
   Dim Ws2 As Worksheet
   
   Set WsM = Sheets("Members")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In WsM.Range("D2", WsM.Range("D" & Rows.Count).End(xlUp))
         ValU = Cl.Value & " " & Cl.Offset(, -1).Value
         If Not .exists(ValU) Then .Add ValU, Cl.Offset(, 1).Value
      Next Cl
      For Each Cl In Ws2.Range("I2", Ws2.Range("I" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 1).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Fluff,

Having trouble wrapping my head around that one, Ill see if I can incorporate that my my code. my not sure how to get the ID number as I need to do some other stuff with it later in the code.
 
Upvote 0
If you have any problems, let me know.
 
Upvote 0
It would help & read your OP properly!
try this
Code:
Sub copydata()

   Dim Cl As Range
   Dim ValU As String
   Dim WsM As Worksheet
   Dim Ws2 As Worksheet
   
   Set WsM = Sheets("Members")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In WsM.Range("C2", WsM.Range("C" & Rows.Count).End(xlUp))
         ValU = Cl.Value & " " & Cl.Offset(, 1).Value
         If Not .exists(ValU) Then .Add ValU, Cl.Offset(, -2).Value
      Next Cl
      For Each Cl In Ws2.Range("I2", Ws2.Range("I" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 14).Value = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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