I have a file called !PaymentTemplate.xlsx and a folder called "Output" with many files. I would like to loop through each of the files in the Output folder and copy data into the !PaymentTemplate.xlsx file and then save that template file. I do not want to append the data, just copy and paste it over and then save that file as a new name and then start over again from the original !PaymentTemplate.xlsx file. Example: A file in the Output folder called "Sunrise.xlsx", the macro should open it, copy the data into !PaymentTemplate.xlsx and save as Sunrise_New.xlsx. Then move on to the next file and do the same thing in the !PaymentTemplate.xlsx file.
If I have 10 files in the Output Directory, there should be 10 new files made with "_New" as part of the file name.
Below is a Macro I have written but am having trouble getting the fileName variable to be part of the Save As Export and testing it.
Sub Energy_Template()
'Loop through all files in a folder
Dim fileName As Variant
fileName = Dir("C:\Dan\Energy Commission\raw data\TEmplate for Upload\Output\")
While fileName <> ""
Workbooks.Open fileName:= _
"C:\Dan\Energy Commission\raw data\TEmplate for Upload\!PaymentTemplate.xlsx"
Workbooks.Open fileName
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("!PaymentTemplate.xlsx").Activate
Range("A3").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir "C:\Dan\Energy Commission\raw data\TEmplate for Upload\new ouput"
ActiveWorkbook.SaveAs fileName:= _
"C:\Dan\Energy Commission\raw data\TEmplate for Upload\new ouput\Sunrise_New.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
ActiveWindow.Close
'Set the fileName to the next file
fileName = Dir
Wend
End Sub
Thanks!