Looping through a range of cells using xlookup

Bobstar

New Member
Joined
Oct 7, 2020
Messages
20
Office Version
  1. 2019
Platform
  1. Windows
Hi all

From source data, I have two columns, client id and names.

I have a second tab with a range of clients ids and for each client id in that range I would like vba to loop through and apply xlookup to get the name from the source data.

Any help is much appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The really fast way to do this is using the VBA dictionary , Try this code :
I have assume the on sheet1 you have cclient Id in coluimn A and Name in column B
on sheet 2 I assumed you have clinent ID on column A
VBA Code:
Sub dictionarylookup()
' this shows the use of a dictionary to copy a value to a mathcing worksheet
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   Dim Cl As Range
   Lastcol = 2
   Set Dic = CreateObject("Scripting.dictionary")
   With ActiveWorkbook.Sheets("sheet1")
   LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
   Ary = .Range(.Cells(1, 1), .Cells(LastRow, Lastcol))
   End With
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 2)
   Next i
   With ActiveWorkbook.Sheets("Sheet2")
   LastRow2 = .Cells(Rows.Count, "A").End(xlUp).Row
   ary2 = .Range(.Cells(1, 1), .Cells(LastRow2, 1))
      For i = 1 To UBound(ary2)
         .Range(.Cells(i, 2), .Cells(i, 2)).Value = Dic(ary2(i, 1)) 'this matches the value given by the index ary2 in the dictionary
      Next i
   End With
End Sub
 
Upvote 0
Solution
The really fast way to do this is using the VBA dictionary , Try this code :
I have assume the on sheet1 you have cclient Id in coluimn A and Name in column B
on sheet 2 I assumed you have clinent ID on column A
VBA Code:
Sub dictionarylookup()
' this shows the use of a dictionary to copy a value to a mathcing worksheet
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   Dim Cl As Range
   Lastcol = 2
   Set Dic = CreateObject("Scripting.dictionary")
   With ActiveWorkbook.Sheets("sheet1")
   LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
   Ary = .Range(.Cells(1, 1), .Cells(LastRow, Lastcol))
   End With
   For i = 1 To UBound(Ary)
      Dic(Ary(i, 1)) = Ary(i, 2)
   Next i
   With ActiveWorkbook.Sheets("Sheet2")
   LastRow2 = .Cells(Rows.Count, "A").End(xlUp).Row
   ary2 = .Range(.Cells(1, 1), .Cells(LastRow2, 1))
      For i = 1 To UBound(ary2)
         .Range(.Cells(i, 2), .Cells(i, 2)).Value = Dic(ary2(i, 1)) 'this matches the value given by the index ary2 in the dictionary
      Next i
   End With
End Sub
Thanks. This works very well
 
Upvote 0
Would this also work for you?
I have assumed that on Sheet2 the IDs start in cell A2.

VBA Code:
Sub Get_Name()
  With Sheets("Sheet2").Range("B2:B" & Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=XLOOKUP(A2,Sheet1!A:A,Sheet1!B:B,"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Thanks. This works very well
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,181
Messages
6,123,508
Members
449,101
Latest member
mgro123

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