I have files in a folder and I want to copy data from these files and paste them into another Master workbook sheet.
I keep getting a runtime error ‘1004’
: Sorry we couldn’t find C:\Users\jjordan\Desktop\Test Dir\MASTER`, It is possible it was moved, renamed or deleted.
The error is highlighted on this line of code: Workbooks.Open SumPath & SumName
I have seen other questions similar to this on the web, I have tried making various changes. But still without success. Please advise.
- Dir for source files:
C:\Users\ jjordan \Desktop\Test Dir\GA Test\
- Dir for Master file:
C:\Users\ jjordan \Desktop\Test Dir\MASTER\
- Source filenames differ, but all end in
"*.xlsx."
- Master filename:
" MASTER – Data List - 2016.xlsm "
‘macro file - Source worksheet name =
"Supplier_Comments"
Master worksheet name =
"Sheet5"
Option Explicit Sub GetDataFromMaster() Dim MyPath As String Dim SumPath As String Dim MyName As String Dim SumName As String Dim MyTemplate As String Dim SumTemplate As String Dim myWS As Worksheet Dim sumWS As Worksheet 'Define folders and filenames MyPath = "C:\Users\jjordan\Desktop\Test Dir\GA Test\" SumPath = "C:\Users\jjordan\Desktop\Test Dir\MASTER\" MyTemplate = "*.xlsx" 'Set the template. SumTemplate = "MASTER – Data List - 2016.xlsm" 'Open the template file and get the Worksheet to put the data into SumName = Dir(SumPath & SumTemplate) Workbooks.Open SumPath & SumName Set sumWS = ActiveWorkbook.Worksheets("Sheet5") 'Open each source file, copying the data from each into the template file MyName = Dir(MyPath & MyTemplate) 'Retrieve the first file Do While MyName <> "" 'Open the source file and get the worksheet with the data we want. Workbooks.Open MyPath & MyName Set myWS = ActiveWorkbook.Worksheets("Suppliers_Comment") 'Copy the data from the source and paste at the end of sheet 5 myWS.Range("A2:N100").Copy sumWS.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'Close the current sourcefile and get the next Workbooks(MyName).Close SaveChanges:=False 'close MyName = Dir 'Get next file Loop 'Now all sourcefiles are copied into the Template file. Close and save it Workbooks(SumName).Close SaveChanges:=True End Sub
MyPath = CreateObject("WScript.Shell").specialfolders("Desktop") & "\Test Dir\GA Test\"
– Darren Bartrup-Cook