Error Received: Run-Time error '1004'. Application-defined or object-defined error. I've searched the forum for similar topics but could never get a good answer. I am trying to extract data from a closed workbook. Here is sample of my code. I need the last row from the source data, but there are many workbooks and each one will have a different last row.
Option Explicit
Sub CopyFromClosedWorkbook2()
' Defines variables
Dim wb1 As Workbook, wb2 As Workbook
Dim fname As String, fpath As String, shtname As String
Dim tgtlr As Long
Dim current_lcn As Long
Dim current_row As Long
Dim i As Long: i = 2
' Disable screen updating to reduce screen flicker
Application.ScreenUpdating = False
' Define which workbook is which
current_lcn = Cells(2, 3).Value
Set wb1 = Workbooks("RBOM test.xlsm")
tgtlr = Cells(Rows.Count, 3).End(xlUp).Row
fpath = "C:\users\folder1\My Documents\folder2\folder3\"
fname = "filename.xlsx": shtname = "filename"
With ThisWorkbook.Sheets("Sheet1").Range("AQ2:FO2")
.Formula = "=INDEX('" & fpath & "[" & fname & "]" & shtname & "'!A:A, COUNTA('" & fpath & "[" & fname & "]" & shtname & "'!$A:$A)-1)"
.Value = .Value
End With
' Re-enable screen updating
Application.ScreenUpdating = True
End Sub
the filename is the same as the shtname
I eventually want to get it so where it'll loop through the rest of the files. But since I've haven't found a way to do this without opening the workbook. I've read that someone developed a code where you didn't have to open the workbook, but you needed the range pre determined, whereas mine is different depending on the last row. The last part of the code was my attempt at the beginning of incorporating the code into a for loop and pasting the data down with the same criteria.