Run time error 1004 for dynamic vlookup vba

Starkers

New Member
Joined
Oct 26, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi All

I would like to create a dynamic vlookup across two workbooks; Book1 and Book2.

Book1 is the source file and Book2 is the file which I would like my values to be paste on.

The vlookup formula would be =VLOOKUP(K2,'C:\Users\Desktop\Mar22\[Book1.xlsx]Sheet3'!$E:$I,5,0)

However, the code below had produced an error:

VBA Code:
Sub MakeFormulas()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet


'Directory of source file
Set sourceBook = Workbooks.Open("C:\Users\Desktop\Mar22\Book1.xlsx")

'Worksheet names
Set sourceSheet = sourceBook.Worksheets("Sheet3")
Set outputSheet = ThisWorkbook.Worksheets("TopSegments")


With sourceSheet
    SourceLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row


   .Range("K2:K" & OutputLastRow).Formula = _
        "=VLOOKUP(F2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$E$2:$I$" & SourceLastRow & ",5,0)"


End With

End Sub
Run-time error '1004':
Application-defined or object-defined error
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Have to run out for a while, but thought I'd suggest to check that the wb is not protected or already open. The latter can occur if it was opened in code in a prior attempt and not properly terminated. For that, check Task Manager before running the code. Note that you have to look in Processes as well as Applications list in TM.
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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