简体   繁体   English

我应该使用 auto_open/close 还是 workbook_open/beforeclose 在打开时运行 VBA?

[英]Should I use auto_open/close or workbook_open/beforeclose to run VBA on open?

In Excel 2003/2007, in which cases should I use auto_open/auto_close routines, and in which cases should I use the workbook_open/workbook_beforeclose events instead?在 Excel 2003/2007 中,在哪些情况下我应该使用 auto_open/auto_close 例程,在哪些情况下我应该使用 workbook_open/workbook_beforeclose 事件?

Apart from backward compatibility to prehistoric versions of Excel, the difference I'm aware of is that one is public and one is private.除了向后兼容史前版本的 Excel 之外,我所知道的区别在于一个是公共的,一个是私有的。 What (if any) are the other differences?其他区别(如果有的话)是什么?

I spent a long time once choosing between the opening variants of these, and we used Auto_Open.我花了很长时间在这些开放变量之间进行选择,我们使用了 Auto_Open。 The main rationale was because if anyone runs the following code:主要理由是因为如果有人运行以下代码:

Application.EnableEvents = False

... then the Workbook_Open event will not fire. ... 那么 Workbook_Open 事件将不会触发。 I had a feeling there was another situation where Workbook_Open didn't work either, but I can't remember it now.我有一种感觉,还有另一种情况 Workbook_Open 也不起作用,但我现在想不起来了。

I don't have a strong opinion about the close events, but to be honest I'd use the Auto_Close there too.我对关闭事件没有强烈的看法,但老实说,我也会在那里使用 Auto_Close。 At the end of the day, it's had more testing.在一天结束时,它进行了更多测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM