简体   繁体   English

如何从工作簿B中调用Workbook_Open on Workbook A?

[英]How can I call Workbook_Open on Workbook A from Workbook B?

My situation is that I am trying to open Workbook A from Workbook B and run the Workbook_Open subroutine. 我的情况是我正在尝试从工作簿B打开工作簿A并运行Workbook_Open子例程。 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. 仅仅使用Workbooks.Open("c:\\myworkbook.xls")是我需要在Workbook_Open子例程运行之前在Workbook A中Workbooks.Open("c:\\myworkbook.xls")数据。

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. 我的两个想法如下。

Option 1: Open Workbook A with macros disabled, plant the data, save and close the workbook then re-open it with macros enabled. 选项1:打开工作簿A,禁用宏,植入数据,保存并关闭工作簿,然后在启用宏的情况下重新打开它。 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. 我不想这样做的原因是:我正在处理一个具有相当数量格式的.xls文件(基本上我模拟人类将如何使用它数千次)并保存文件数千次可能导致文件损坏。 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. 其中一个重点是速度和效率,以及最快的周转时间。

Option 2: Duplicate the Workbook_Open code as a public subroutine in elsewhere within a module. 选项2:将Workbook_Open代码复制为模块中其他位置的公共子例程。 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)

From workbook B: 从工作簿B:

'add vb reference to "Microsoft Visual Basic For Applications Extensibility 5.3"
Sub Tester()

    Dim wb As Workbook
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim LineNum As Long

    Set wb = Workbooks("BookA.xlsm")
    Set VBProj = wb.VBProject
    Set VBComp = VBProj.VBComponents("ThisWorkbook")
    Set CodeMod = VBComp.CodeModule

    With CodeMod
        LineNum = .CountOfLines + 1
        .InsertLines LineNum, "Public Sub Blah()"
        LineNum = LineNum + 1
        .InsertLines LineNum, "   Workbook_Open"
        LineNum = LineNum + 1
        .InsertLines LineNum, "End Sub"
    End With

    Application.Run "BookA.xlsm!ThisWorkbook.Blah"

End Sub

See: http://www.cpearson.com/excel/vbe.aspx 见: http//www.cpearson.com/excel/vbe.aspx

Workbook_Open is a private sub. Workbook_Open是一个私有子。 You cannot call it directly. 你不能直接打电话。

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. 我可以提供选项3:将Workbook_open的代码复制到Workbook B中的子代码中。然后,您可以将其称为公共,然后您可以将其调用以进行模拟。

In addition, if corruption is a concern, create a copy of Workbook A to use for your tests. 此外,如果需要考虑损坏,请创建工作簿A的副本以用于测试。 Then if it gets corrupted, the original will still be available. 然后如果它被破坏,原件仍然可用。

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

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