Trying to use VLookup across workbook worksheets

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
I have tried to use a VLookup code on a worksheet to use VLookup function to find prices on worksheet Parts list then fill this into worksheet Job Card Master using code in column D to fill column O to lastrow.
The code won't run the error says "Application-defined or object-defined error"?

VBA Code:
Private Sub Up_Date_Prices_Click()

        Dim JCM As Worksheet, PartsList As Worksheet
        Dim JCMLastRow As Long, PartsListLastRow As Long, x As Long
        Dim DataRng As Range
       
        
        
        Set JCM = ThisWorkbook.Worksheets("Job Card Master")
        Set PartsList = ThisWorkbook.Worksheets("Parts List")
        
        JCMLastRow = JCM.Range("A" & Rows.Count).End(xlUp).row
        PartsListLastRow = PartsList.Range("A" & Rows.Count).End(xlUp).row
        
        Set DataRng = PartsList.Range("A2:B" & PartsListLastRow)
        
        For x = 13 To JCMLastRow
        
        On Error Resume Next
        
           JCM.Range("O" & x).Value = Application.WorksheetFunction.VLookup( _
                JCM.Range("D" & x).Value.DataRng, 2, False)
        
        Next x
  
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You only have a typo in this line of code:
JCM.Range("O" & x).Value = Application.WorksheetFunction.VLookup(JCM.Range("D" & x).Value.DataRng, 2, False)

should be (note the comma instead of the dot):
JCM.Range("O" & x).Value = Application.WorksheetFunction.VLookup(JCM.Range("D" & x).Value, DataRng, 2, False)

If you Debugged the macro without On Error Resume Next you would've noticed the problem.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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