当我在新工作簿中插入新模板工作表时运行的代码,源文件(主文件)将打开它。
课堂活动
Private Sub App_WorkbookNewSheet(ByVal Wb As Workbook, ByVal Sh As Object)
Application. Run "PERSONAL.XLSB!Open_it"
End Sub
open_it模块
Sub Open_it()
' Open the workbook from its actual folder
Workbooks.Open Filename:="C:\Users\ASUS User\Desktop\MP\source.xlsm"
ThisWorkbook.Activate
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屏幕。
所有这些代码都存储在personal.xlsb
中