如何在 Excel 启动(不是 workbook_open)上运行代码?



我正在寻找一种在打开 excel 文件时运行代码的方法,但不是从 workbook_open 方法运行代码。

就像使用特定名称"main_tables.xlsx"一样,并且在打开时表现出色,运行自定义宏,这些宏不是来自所述文件。

有什么想法吗?谢谢。

您可以使用加载项。

ThisWorkbook模块中使用以下代码创建外接程序:

Private WithEvents app As Application
' This will run when any workbook is opened.
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
' Check if this add-in is opening versus any other workbook.
If Not Wb Is Me Then
' Another workbook is opening - do something...
MsgBox Wb.Name
End If
End Sub
' This will run when the add-in workbook is opened.
Private Sub Workbook_Open()
' Set the app variable so that we can listen for WorkbookOpen event
Set app = Application
End Sub

将其另存为外接程序文件夹中的外接程序(通常为 C:\Users\\AppData\Roaming\Microsoft|加载项(,方法是选择"文件"->"另存为"并选择 Excel 加载项 (*.xlam( 文件类型。

然后在"文件"-">选项"-">加载项"中选择"管理 Excel 加载项",然后按"转到..."。这将打开要自动启动的加载项列表。勾选您刚刚创建的加载项。

现在,当您打开任何工作簿时,加载项代码将运行。

最新更新