1. Create a VBA script to automate the process. Use a VBA script that opens each .xlsx file, copies the data into a new workbook based on the .xltm template, and then saves this new file as .xlsm. This allows you to transfer the data while applying the template's macros. 2. VBA Script Example Here is an overview of the script to be placed in a master Excel file: vba Sub ApplyModel() Dim fso As Object, folder As Object, file As Object Dim wbSource As Workbook, wbModel As Workbook Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder("C:\Work\SourceWorkbook") For Each file In folder.Files If LCase(fso.GetExtensionName(file.Name)) = "xlsx" Then Set wbSource = Workbooks.Open(file.Path) Set wbModel = Workbooks.Add("C:\Templates\ModelMacro.xltm") wbSource.Sheets(1).UsedRange.Copy wbModel.Sheets(1).Range("A1") wbModel.SaveAs Replace(file.Path, ".xlsx", "_macro.xlsm"), FileFormat:=xlOpenXMLWorkbookMacroEnabled wbSource.Close False wbModele.Close False End If Next End Sub 3. Enable Macros and File Access Before running the script, ensure that macros are enabled and that programmatic access to VBA is allowed in Excel settings. Otherwise, the script will be blocked.
No comments yet