我有一个xlam可以在工作簿的打开和关闭中使用。
为了做到这一点,我用下面的代码创建了一个类模块:
''''''''''''''''''''''' Setup Event '''''''''''''''''''''''''''''''''''''''''''''''''
Public WithEvents appevent As Application
''''''''''''''''''''''' Setup Application at Close''''''''''''''''''''''''''''''''''''''''''''
Private Sub appevent_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
MsgBox("The workbook " & Wb.Name & " will close now")
End Sub
''''''''''''''''''''''' Setup Application at Open''''''''''''''''''''''''''''''''''''''''''''
Private Sub appevent_WorkbookOpen(ByVal Wb As Workbook)
MsgBox("The workbook " & Wb.Name & " is now open")
End Sub
然后在“ ThisWorkbook”对象中,我有以下代码:
Dim myobject As New Class1
Sub Workbook_Open()
Set myobject.appevent = Application
End Sub
[安装了加载项后,每当打开文件时都会出现两个消息框,一个在Excel启动时显示,然后在打开文件时出现一个,类似的情况用于关闭。
为什么会这样以及如何避免呢?
未经测试,但请尝试以下操作: