How To - Run VBA Macro Automaticall When Opening Template

bepedicino

Board Regular
Joined
Sep 29, 2014
Messages
73
I have a Excel Template which contains the below macro. How can I run the macro automatically when I open the template file?

----------------------------
Sub PrepSpreadsheet()

Const Ffold As String = "D\Documents\file" 'change as required
Dim Fname As String

Fname = "Product Classification"
Fname = Fname & " - " & Format(Date, "yyyymmdd") & ".xlsm"

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs _
Filename:=Ffold & Application.PathSeparator & Fname, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled

Application.DisplayAlerts = True

End Sub
 
1. Open your template
2. Right click the sheet name tab
3. Click 'View Code' in th pop up menu.
4. Copy this code and paste it into the code pane of the VB editor.
4. Close the VB editor
5. Save the workbook as macro enabled .xlsm
Now when you open the template, the code will automatically run.
Code:
Private Sub Worksheet_Activate()
 Const Ffold As String = "D\Documents\file" 'change as required
 Dim Fname As String
 Fname = "Product Classification"
 Fname = Fname & " - " & Format(Date, "yyyymmdd") & ".xlsm"
 Application.DisplayAlerts = False
 ActiveWorkbook.SaveAs _
 Filename:=Ffold & Application.PathSeparator & Fname, _
 FileFormat:=xlOpenXMLWorkbookMacroEnabled
 Application.DisplayAlerts = True
End Sub
 
Upvote 0
When I attempt to run the code I am getting a Run-Time error '104': Application or object-define error message. I am not sure if this is related to the saving of the file that I need to do.

In step 5 I am being instructed to save the workbooks a .xlsm fie. For clarification, do I need to save my template file as a "blank" .xlsm file? I so, does it need to be in the same location as the file being saved by the macro, or do I save it in the same location as my .xltm file in order for it to work?

Once the macro is functional I should be able to run it via the .xltm file, is that correct?
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top