VBA VLookup Code

WalterSil

New Member
Joined
Nov 30, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I have a workbook which has multiple worksheets and wanted to apply a vlookup formula through VBA. The reference table is on sheet "categ lookup" range A1:C357. The columns are located on worksheet labeled "Original" and need vlookup for columns R and S and the reference data is in column T, all three columns data start on row 4 since headers start on row 3.

regular vlookup code with out VBA would be the following:
=VLOOKUP(T4,'categ lookup'!A1:C357,2,0)
=VLOOKUP(T4,'categ lookup'!A1:C357,3,0)

I need the formula to apply to last row in each column. Data changes, so need last row formula.

Please help, can't seem to figure this one out.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I was able to figure it out after a little research:

Sub Vlookup

'Used to VLookup Data from categ lookup worksheet
Dim origWs As Worksheet, dataWs As Worksheet
Dim origLastRow As Long, dataLastRow As Long, x As Long

Set origWs = ThisWorkbook.Worksheets("Original")
Set dataWs = ThisWorkbook.Worksheets("categ lookup")

origLastRow = origWs.Range("A" & Rows.Count).End(xlUp).Row
dataLastRow = dataWs.Range("A" & Rows.Count).End(xlUp).Row

Set dataRng = dataWs.Range("A2:C" & dataLastRow)

For x = 2 To origLastRow
On Error Resume Next
origWs.Range("R" & x).Value = Application.WorksheetFunction.VLookup( _
origWs.Range("T" & x).Value, dataRng, 2, False)
origWs.Range("S" & x).Value = Application.WorksheetFunction.VLookup( _
origWs.Range("T" & x).Value, dataRng, 3, False)
Next x


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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