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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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