简体   繁体   中英

Excel template's VBA code not working after I insert it into a new workbook

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而不是标准的.xltx模板扩展。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM