Help with using VLookup in a Loop using VBA

JohnGow383

Board Regular
Joined
Jul 6, 2021
Messages
141
Office Version
  1. 2013
Platform
  1. Windows
Hi, I need help with the following.
I have a table in Sheet1 and a data table in Sheet2.
I want the 2nd column C of Table 1 to populate from the value of the adjacent cell in column B by looking up the master table in Sheet2, column T to read value in column U
I realize this can easily be done using the Vlookup formula but would prefer a VBA solution.
I will then have the macro fired from a worksheet change event using the range B2:B22 in sheet 2. So any change in that column will re-calculate Column C.
Here is the code I have but it doesn't work so any help will be appreciated.

VBA Code:
Sub PlayLookUp()

Dim PlayGest As String
Dim PlayList As Range
Dim LoopList, Pokemon As Range
Dim lrT As Long

Set ws1 = Sheet1
Set ws2 = Sheet2

Set LoopList = ws1.Range("B2:B22")

lrT = ws2.Cells(Rows.Count, "T").End(xlUp).Row
Set PlayList = ws2.Range(ws2.Cells(4, "T"), ws2.Cells(lrT, "U"))

For Each Pokemon In LoopList
    PlayGest = Application.WorksheetFunction.VLookup(Pokemon.Value, PlayList, 2, False)
      Pokemon.Offset(0, 1).Value = PlayGest
 Next Pokemon
End Sub

Capture.JPG
Capture1.JPG
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Thanks for posting on the forum.

I prefer to use the Find method:

VBA Code:
Sub PlayLookUp()
  Dim c As Range, f As Range
  For Each c In Sheets("Sheet1").Range("B2", Sheets("Sheet1").Range("B" & Rows.Count).End(3))
    Set f = Sheets("Sheet2").Range("T:T").Find(c.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      c.Offset(, 1).Value = f.Offset(, 1).Value
    End If
  Next
End Sub


Without loop:

VBA Code:
Sub PlayLookUp2()
  With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Range("B" & Rows.Count).End(3).Row)
    .Formula = "=IFERROR(vlookup(B2,Sheet2!T:U,2,0),"""")"
    .Value = .Value
  End With
End Sub

Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor

----- --
 
Last edited:
Upvote 1
Solution
Hi,

Thanks for the reply. When I reied that I was getting Error 9, subscript out of range in the line
VBA Code:
[QUOTE]
Set f = Sheets("Sheet2").Range("T:T").Find(c.Value, , xlValues, xlWhole, , , False)
[/QUOTE]

There is nothing below the table in Sheet2, however there are populated cells in Sheet 1 below Range("B2:B22")
Thanks for posting on the forum.

I prefer to use the Find method:

VBA Code:
Sub PlayLookUp()
  Dim c As Range, f As Range
  For Each c In Sheets("Sheet1").Range("B2", Sheets("Sheet1").Range("B" & Rows.Count).End(3))
    Set f = Sheets("Sheet2").Range("T:T").Find(c.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      c.Offset(, 1).Value = f.Offset(, 1).Value
    End If
  Next
End Sub


Without loop:

VBA Code:
Sub PlayLookUp2()
  With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Range("B" & Rows.Count).End(3).Row)
    .Formula = "=IFERROR(vlookup(B2,Sheet2!T:U,2,0),"""")"
    .Value = .Value
  End With
End Sub

Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor

----- --
 
Upvote 0
Thanks for posting on the forum.

I prefer to use the Find method:

VBA Code:
Sub PlayLookUp()
  Dim c As Range, f As Range
  For Each c In Sheets("Sheet1").Range("B2", Sheets("Sheet1").Range("B" & Rows.Count).End(3))
    Set f = Sheets("Sheet2").Range("T:T").Find(c.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      c.Offset(, 1).Value = f.Offset(, 1).Value
    End If
  Next
End Sub


Without loop:

VBA Code:
Sub PlayLookUp2()
  With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Range("B" & Rows.Count).End(3).Row)
    .Formula = "=IFERROR(vlookup(B2,Sheet2!T:U,2,0),"""")"
    .Value = .Value
  End With
End Sub

Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor

----- --

Sorry, my mistake, it works perfectly. I am not used to using the actual sheet name and didn't change the sheet name. I usually use the code name. When I ran it it worked fine. Thanks for this, very much appreciated.
 
Upvote 1

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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