To create a new Excel file using a template file (VBA), follow these steps:
-
First, ensure you have a template Excel file (.xltx or .xltm) in the specified path. Let's assume the template file is located at
C:\TemplateFiles\Template.xltx. -
Next, create a new Excel file based on the template.
Sub CreateNewExcelFile()
Dim myFile As Object
Set myFile = CreateObject("Excel.Application")
' Set display options (optional)
myFile.Visible = False
myFile.DisplayAlerts = False
' Open the template file
Set myWorkbook = myFile.Workbooks.Open(Filename:="C:\TemplateFiles\Template.xltx")
' Save the template as a new workbook
myWorkbook.SaveAs Filename:="C:\NewFiles\NewFile.xlsx"
' Close the template workbook
myWorkbook.Close
' Quit Excel
myFile.Quit
' Release the objects
Set myFile = Nothing
Set myWorkbook = Nothing
End Sub
This VBA code creates a new Excel file based on the specified template file and saves it in a new location. Adjust the file paths as needed.