如何从工作簿B调用工作簿A上的Workbook_Open? [英] How can I call Workbook_Open on Workbook A from Workbook B?

查看:87
本文介绍了如何从工作簿B调用工作簿A上的Workbook_Open?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是我试图从工作簿B中打开工作簿A并运行Workbook_Open子例程.仅使用Workbooks.Open("c:\myworkbook.xls")的问题在于,我需要在Workbook_Open子例程运行之前将数据植入工作簿A.

My situation is that I am trying to open Workbook A from Workbook B and run the Workbook_Open subroutine. The issue with just simply using Workbooks.Open("c:\myworkbook.xls") is that I need to plant data in Workbook A before the Workbook_Open subroutine runs.

我有两个想法,我都不喜欢,所以我想知道是否有更好的方法.我的两个想法如下.

I have two ideas, neither of which I particularly like, so I was wondering if there was a better way. My two ideas are as follows.

选项1:在禁用宏的情况下打开工作簿A,植入数据,保存并关闭工作簿,然后在启用宏的情况下重新打开它.我不想这样做的原因是:我正在处理具有大量格式的.xls文件(基本上,我正在模拟人类将如何使用它数千次),并且将文件保存数千次可能会导致文件损坏.此外,这需要花费相当多的时间才能两次打开工作簿,对我而言似乎根本没有效率.重点之一就是速度和效率以及最快的周转时间.

Option 1: Open Workbook A with macros disabled, plant the data, save and close the workbook then re-open it with macros enabled. The reasons I would prefer not to do this is: I am dealing with a .xls file that has a fair amount of formatting (basically I am simulating how a human would use it thousands of times over) and saving the file thousands of times may cause the file to corrupt. Additionally this takes a fair amount of time to open the workbook twice, and it does not seem at all efficient to me. One of the big points of emphasis is speed and efficiency with the quickest turnaround time.

选项2:将Workbook_Open代码作为公共子例程复制到模块中其他位置.这是两者中更可取的,但是问题是我不一定具有执行此操作的权限这样做会涉及大量繁文tape节,危险标志等.

Option 2: Duplicate the Workbook_Open code as a public subroutine in elsewhere within a module. This is the more desirable of the two, but the issue is I do not necessarily have permission to perform this action and doing this will involve plenty of red tape, red flags, etc.

有什么办法可以做这样的事情:

Is there any way to do something like this:

Workbooks("Workbook A").Application.Run (Workbooks("Workbook A").Workbook_Open)

推荐答案

Workbook_Open是私有子项.您不能直接调用它.

Workbook_Open is a private sub. You cannot call it directly.

我可以提供选项3:将Workbook_open中的代码复制到工作簿B的子目录中.然后您可以将其称为public,并可以将其用于仿真.

May I offer Option 3: Copy the code from Workbook_open into a sub in Workbook B. Then you can call it will be public and you can call it for your simulation.

此外,如果担心损坏,请创建工作簿A的副本以用于测试.然后,如果它损坏了,原始文件仍然可用.

In addition, if corruption is a concern, create a copy of Workbook A to use for your tests. Then if it gets corrupted, the original will still be available.

这篇关于如何从工作簿B调用工作簿A上的Workbook_Open?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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