I am trying to fill a range of cells with a XLOOKUP formula using VBA.
I will be using a different file every day, so I would like to use a variable for the date portion of the filename.
I get
Run-time error 1004: Application-defined or object-defined error
in the last step of filling in column V with the XLOOKUP formula.
Dim lastRow As Long, filename As String, reportDate As String
Call getReportDate(reportDate) 'UserForm where user enters date of report
lastRow = ActiveSheet.Range("J" & Sheets("OCI").Rows.Count).End(xlUp).Row
filename = "report_name_" & reportDate & ".xlsx"
Range("V2:V" & lastRow).Select
Selection.Value = "=XLOOKUP(@A:A, ""[" & filename & "]Sheetname""!$L:$L, ""[" & filename & "]Sheetname""!$N:$N)"
Hard coding the filename works.
Range("V2:V" & lastRow).Formula= "=XLOOKUP(@A:A, [" & filename & "]Sheetname!$L:$L, [" & filename & "]Sheetname!$N:$N)"