当用户在Excel中启用宏时,触发我的宏代码 [英] Trigger my macro code when user enables macros in Excel
问题描述
如果用户选择启用宏,那么我的宏应该响应那个运行。
首先,关于你的默认不是一个选项声明,请记住,这不一定是真的。用户可以允许所有的宏自动执行,无需任何提示(在开发人员选项卡
,宏安全性
下,选择启用所有宏(不推荐...))。如果默认情况下未启用宏,则在用户允许运行给定宏之前,您无法执行任何操作。宏运行后,您可以在开始时自动触发各种事件,您可以在其中放置代码。例如:在文件 ThisWorkbook
(Microsoft Excel Objects)下,您可以使用 Workbook_Open
方法。示例代码:
Private Sub Workbook_Open()
MsgBox这是我可以弹出的最快
End Sub
如果您在上述文件中复制此代码,您会看到一个弹出窗口出现在宏已被启用。
注意:有自己的文件类型(.xlsm)的事实应该足够指示。不幸的是,有不少人认为宏的功能非常有限(主要限于给定的电子表格),什么是危险的误解:一个宏可以做的几乎与传统的.exe文件相同。
As macros are disabled by default (enabling them by default is not an option) whenever I open my excel worksheet containing macros I get a popup asking if I want to enable macros. Is it possible to catch the 'OK' button press event of this popup and run a macro.
I.e. if the user chooses to enable macros then my macro should run in response to that.
Firstly and regarding your "enabling them by default is not an option" statement, bear in mind that this is not necessarily true. The user can allow all the macros to be automatically executed without any prompt (under the Developer tab
, Macro security
, select "Enable all macros (not recommended...)"). In case of not having the macros enabled by default, you cannot do anything before the user allows the given macro to run. Once the macro is running, you have various events being automatically triggered right at the start, where you can put your code. For example: in the file ThisWorkbook
(under "Microsoft Excel Objects"), you can use the Workbook_Open
method. Sample code:
Private Sub Workbook_Open()
MsgBox "This is the soonest I can popup"
End Sub
If you copy this code in the aforementioned file, you would see a popup appearing right after the macros has been enabled.
NOTE: the fact of having its own file type (.xlsm) should be indicative enough. Unfortunately, quite a few people think that macros have very limited functionalities (mainly restricted to the given spreadsheet), what is a dangerous misconcepcion: a macro can do virtually the same things than a conventional .exe file.
这篇关于当用户在Excel中启用宏时,触发我的宏代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!