简体   繁体   English

我将其插入新工作簿后,Excel模板的VBA代码不起作用

[英]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. 我创建一个具有固定格式和VBA存储的Excel模板。

Whenever I insert the Excel template into a new workbook, the VBA code will not follow and apply. 每当我将Excel模板插入新工作簿时,VBA代码都不会遵循并应用。 Only the format follows. 仅遵循格式。

Does anyone know why? 有人知道为什么吗?

This is the code stored in Template.xltm. 这是存储在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? excel宏中的VBA代码是否启用模板?

您需要确保将工作簿另存为启用了宏的模板,文件扩展名为.xltm而不是标准的.xltx模板扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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