Trying to Vookup another workbook

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
The code below seems to not be able to Vlookup the other workbook?
It says Run Time Error 1004 on this line??

VBA Code:
                    JCM.Range("A4").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 40, 0)
                      Range("A4").Select


VBA Code:
Private Sub Fill_Details_From_Job_Record_Click()
                
               TurnOff
               
                Application.ScreenUpdating = False
                
                    
                        Dim SrcOpen As Workbook, Des As Workbook
                        Dim JCM As Worksheet, TGSR As Worksheet, JobCard As Worksheet
                        Dim FilePath As String, Filename As String
                        Dim JobCardData As Range, SrcDataRange As Range
                        Dim LastRow As Long
                      
                        FilePath = "\\tgs-srv01\share\ShopFloor\PRODUCTION\JOB BOOK\"
                        Filename = "JOB RECORD SHEET.xlsm"
                        
           
                        Set SrcOpen = Workbooks.Open(FilePath & Filename)
                        Set TGSR = SrcOpen.Worksheets("TGS JOB RECORD")
                        LastRow = TGSR.Cells(TGSR.Rows.Count, "A").End(xlUp).Row
                        Set SrcDataRange = TGSR.Range("A2:AN" & LastRow)
                   
                        Set JCM = ThisWorkbook.Worksheets("Job Card Master")
                        Set JobCard = ThisWorkbook.Worksheets("Job Card with Time Analysis")

                        
'                      On Error GoTo ErrHandler
                                   
                    JCM.Range("A4").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 40, 0)
                      Range("A4").Select
                  
                    JCM.Range("C4").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 8, 0)
                      Range("C4").Select
                    
                    JCM.Range("D4").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 33, 0)
                      Range("D4").Select
                    
                    JCM.Range("F6").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 18, 0)
                      Range("F6").Select
                      
                    JCM.Range("A8").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 2, 0)
                      Range("A8").Select
                      
                    JCM.Range("E8").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 3, 0)
                      Range("E8").Select
                      
                    JCM.Range("G8").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 5, 0)
                      Range("G8").Select
                      
                    JCM.Range("K10").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 7, 0)
                      Range("K10").Select
                      
                    JCM.Range("K8").Value = Application.WorksheetFunction.VLookup(JCM.Range("G2"), SrcDataRange, 4, 0)
                      Range("K8").Select
                      
                    JobCard.Range("A4").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 40, 0)
                      Range("A4").Select
                  
                    JobCard.Range("C4").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 8, 0)
                      Range("C4").Select
                    
                    JobCard.Range("D4").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 33, 0)
                      Range("D4").Select
                    
                    JobCard.Range("F6").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 18, 0)
                      Range("F6").Select
                      
                    JobCard.Range("A8").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 2, 0)
                      Range("A8").Select
                      
                    JobCard.Range("E8").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 3, 0)
                      Range("E8").Select
                      
                    JobCard.Range("G8").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 5, 0)
                      Range("G8").Select
                      
                    JobCard.Range("K10").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 7, 0)
                      Range("K10").Select
                      
                    JobCard.Range("K8").Value = Application.WorksheetFunction.VLookup(JobCard.Range("G2"), SrcDataRange, 4, 0)
                      Range("K8").Select
                      
                      Application.DisplayAlerts = False
                      SrcOpen.Close
                      Application.DisplayAlerts = True
                            
                      Application.ScreenUpdating = True
                      
'ErrHandler:
'                       If Err = 1004 Then
'                       MsgBox "Fill Job Number in Job Card Master Sheet Cell G2"
'                       End If
'
                      TurnOn

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Sorry, I found the mistake. The lookup value on the source workbook has been deleted but I used one that is there and it worked.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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