I create an Excel template with fixed format and VBA stored in.
Whenever I insert the Excel template into a new workbook, the VBA code will not follow and apply. Only the format follows.
Does anyone know why?
This is the code stored in Template.xltm. Under ThisWorkbook
Sub Auto_Open()
Workbooks.Open Filename:="C:\Users\ASUS User\Desktop\MP\source.xlsm"
ThisWorkbook.Activate
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Columns(2).AutoFit
Rows().AutoFit
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wBook As Workbook
On Error Resume Next
Set wBook = Workbooks("source.xlsm")
If wBook Is Nothing Then
'The Workbook Is Not open'
' Nothing to do as workbook is not open
Set wBook = Nothing
On Error GoTo 0
Else 'Workbook is open
' Close the workbook and save any changes
Workbooks("source.xlsm").Close SaveChanges:=True
Set wBook = Nothing
On Error GoTo 0
End If
End Sub
Does VBA code in excel macro enable template follows the template?
.xltm
workbook? Please read How to Ask and edit your question to include more detail about your problem. As it currently stands it is very difficult to know why your template is not working as desired. Try and create a minimal reproducible example so we can test it on our own machines and give a sample of your VBA code. – girlvsdata