Excel宏Auto_Open()仅在第一次打开 [英] Excel Macro Auto_Open() only opens first time

查看:1045
本文介绍了Excel宏Auto_Open()仅在第一次打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我开发了一个Excel工作簿,可以自动执行我们公司运行的测试任务。同事可以使用此工作簿为测试创建模板。打开时,它运行以下宏(在模块中):

I developed a Excel Workbook that automates some task for tests we run in our company. Colleagues can create templates for tests using this workbook. Upon opening, it runs the following macro (in a module):

Public Sub Auto_Open()

    On Error Resume Next
    
    Application.Run ("CreateCollectDataBTN")
    Application.Run ("CreateSaveFileBTN")
    Application.Run ("ReloadDir")
    
    On Error GoTo 0

End Sub

关闭后,它会运行Auto_Close()来关闭一些内容并正确保存文件。

Upon closing, it runs a Auto_Close() to close some things and save the file correctly.

这适用于所有人。现在,我的公司迁移到Office 365存储,由于某种原因,Auto_Open脚本仅在有人打开脚本时才运行。所以,我打开一个文件,运行它需要运行的宏。然后,当我再次打开模板时,
它不会运行Auto_Open宏,而是运行Auto_Close宏。如果我然后将文件重命名为File.v2.xlsm它再次运行正确的宏。但是,关闭它并重新打开文件时,除非重命名文件,否则它不再起作用。这个
行为也发生在同事的笔记本电脑上。

This worked for everyone. Now, my company migrated to Office 365 storage, and for some reason, the Auto_Open script only runs the first time someone opens the script. So, I open a file, it runs the macros it needs to run. Then, when I open the template again, it does not run the Auto_Open macro but it runs the Auto_Close macro instead. If I then rename the file to File.v2.xlsm it runs the correct macros again. However, when closing it and reopening the file, it does not work anymore unless I rename the file. This behavior also occurs on laptops of colleagues.

我是否需要更改设置以防止此行为?

Is there a setting I need to change in order to prevent this behavior?

感谢您的帮助,

Hans




推荐答案

Auto_Open是旧技术。我认为它仍然有效,但也许它会给以后版本的Excel带来问题。替换(当前技术)是使用事件代码。

Auto_Open is old technology. I thought that it was still working but maybe it is giving problems with later versions of Excel. The replacement (Current technology) is to use event code.

删除您拥有的Auto_Open子,然后在VBA编辑器中,双击Project Explorer中的ThisWorkbook(编辑器中的左列) )。 

Remove the Auto_Open sub that you have and then in the VBA editor, double click ThisWorkbook in the Project Explorer (Left column in the editor). 

然后插入以下代码。 请注意,我更改了子名称,您必须保留该名称,以便在打开工作簿时运行代码。

Then insert the following code. Note that I have changed the sub name and you must keep that name for the code to run when the workbook is opened.

Private Sub Workbook_Open()

Private Sub Workbook_Open()

    On Error Resume Next

   

    Application.Run(" CreateCollectDataBTN")

    Application.Run(" CreateSaveFileBTN")

    Application.Run(" ReloadDir")

   

    On Error GoTo 0

    On Error Resume Next
   
    Application.Run ("CreateCollectDataBTN")
    Application.Run ("CreateSaveFileBTN")
    Application.Run ("ReloadDir")
   
    On Error GoTo 0

End Sub


这篇关于Excel宏Auto_Open()仅在第一次打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆