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

时间:2018-08-13 06:19:50

标签: excel vba excel-2010

我创建具有固定格式和VBA存储的Excel模板。

每当我将Excel模板插入新工作簿时,VBA代码将不会遵循并应用。仅遵循格式。

有人知道为什么吗?

这是存储在Template.xltm中的代码。在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

Excel宏启用模板中的VBA代码是否遵循该模板?

1 个答案:

答案 0 :(得分:0)

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