简体   繁体   English

Excel VBA宏跨工作簿未完全执行

[英]Excel VBA macro accross workbooks not fully executed

I have 6 files that execute macros each after another. 我有6个文件,它们一个接一个地执行宏。 The 1st file with VBA code (master file) opens remaining 5 files and initiates VBA chain reaction. 带有VBA代码的第一个文件(主文件)将打开剩余的5个文件,并启动VBA链反应。 The 2nd file does the macro job and initiates job of the 3rd file and it goes until 6th file. 第二个文件执行宏作业并启动第三个文件的作业,直到第6个文件。

The 6th file then refers to the 1st file's macro (master), which is supposed to close all 5 workbooks (apart from this 1stone). 然后,第6个文件引用第1个文件的宏(主文件),该宏应关闭所有5个工作簿(除了该1个石头之外)。 The code of 6th workbook looks like this: 第六个工作簿的代码如下所示:

Application.DisplayAlerts = False

ThisWorkbook.RefreshAll
Workbooks("6th_file.xlsm").SaveAs Filename:= _
"[URL]6th_file_htm.htm" 

Application.Run ("refresh_tool.xlsm!CloseAll.CloseAll")

Then it goes to: 然后转到:

Option Explicit

Sub CloseAll()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wb3 As Workbook
Dim wb4 As Workbook
Dim wb5 As Workbook

Set wb1 = Workbooks("wb1.xlsm")
Set wb2 = Workbooks("wb2.xlsm")
Set wb3 = Workbooks("wb3.xlsm")
Set wb4 = Workbooks("wb4.xlsm")
Set wb5 = Workbooks("wb5.xlsm")

wb1.Close
wb2.Close
wb3.Close
wb4.Close
wb5.Close

The result is that when run in that chain only the first workbook is closed (refresh_tool, wb2, wb3, wb4 and wb5 remain open). 结果是,在该链中运行时,仅第一个工作簿处于关闭状态(refresh_tool,wb2,wb3,wb4和wb5保持打开状态)。

However, when the CloseAll() is run alone - it works properly and closes all 5 files (only refresh_tool remains open). 但是,当CloseAll()单独运行时-它将正常工作并关闭所有5个文件(仅refresh_tool保持打开状态)。

I tried make it simple at first with Workbooks("...").Close, but tried several things already, ending up with the code above. 最初,我尝试使用Workbooks(“ ...”)使其变得简单。关闭,但是已经尝试了几件事,最后得到了上面的代码。

Can anyone help please? 有人可以帮忙吗?

This isn't going to work. 这行不通。 You need to do as the other users have suggested. 您需要按照其他用户的建议进行操作。

What is happening is this (condensed to only 3 workbooks): 这是怎么回事(仅压缩为3个工作簿):

i) Run Macro from refresh_tool to open wb1 i)从refresh_tool运行宏以打开wb1

ii) wb1 Opens (in thread for refresh_tool macro) ii)wb1打开(在refresh_tool宏的线程中)

iii) Run Macro from wb1 (in thread for refresh_tool macro) iii)从wb1运行宏(在refresh_tool宏的线程中)

iv) Macro from wb1 is running (in thread for wb1 which is in thread for refresh_tool) iv)来自wb1的宏正在运行(在wb1的线程中,而在refresh_tool的线程中)

v) wb2 Opens (in thread for wb1 which is in thread for refresh_tool) v)wb2打开(在wb1的线程中,而在refresh_tool的线程中)

vi) Run Macro from wb2 (in thread for wb1 macro which is in thread for refresh_tool) vi)从wb2运行宏(在wb1宏的线程中,而在refresh_tool的线程中)

vii) Macro from wb2 calls Close macro in refresh_tool (in thread for wb2 which is in thread for wb1 which is in thread for refresh_tool) vii)来自wb2的宏调用refresh_tool中的Close宏(在wb2的线程中,在wb1的线程中,在refresh_tool在线程中)

viii) Close macro from refresh_tool starts running (in thread for wb2 which is in thread for wb1 which is in original thread for refresh_tool) viii)从refresh_tool开始的关闭宏开始运行(在wb2的线程中,而在wb1的线程中,在refresh_tool的原始线程中)

ix) Close macro from refresh_tool closes wb1 ix)从refresh_tool关闭宏关闭wb1

THIS IS THE PROBLEM!!!!!! 这就是问题!!!!!! This closes the thread for wb1, which ends the running of the close macro in the refresh tool. 这将关闭wb1的线程,从而结束刷新工具中close宏的运行。 So, it never gets past the first close. 因此,它永远都不会超过第一个收盘价。 Even if you reordered how the workbooks were closed, the close macro would be running in the threads for ALL of the workbooks at this point. 即使重新排序了工作簿的关闭顺序,此时,close宏仍将在所有工作簿的线程中运行。 Whichever one you close ends the thread for that workbook, which ends the Close macro. 无论您关闭哪一个,都结束该工作簿的线程,从而结束Close宏。

You need to do what was suggested in the comments and run all the code from one master. 您需要执行注释中建议的操作,并从一个主服务器运行所有代码。 Alternatively (and I would recommend the other way), close each workbook at the end of each macro within the workbook if you feel you really must chain these: 或者(如果相反,我会建议),如果您确实必须链接这些工作簿,请在工作簿中每个宏的末尾关闭每个工作簿:

In wb1: 在wb1中:

Public Sub openFileandRun()

Dim wb As Workbook

Set wb = Workbooks.Open("filepath\wb2.xlsm")
Application.Run (wb.Name & "!openModule.openFileandRun")
Thisworkbook.Close SaveChanges:=False

End Sub

In wb2: 在wb2中:

Public Sub openFileandRun()

Dim wb As Workbook

Set wb = Workbooks.Open("filepath\wb3.xlsm")
Application.Run (wb.Name & "!openModule.openFileandRun")
Thisworkbook.Close SaveChanges:=False

End Sub

Etc. 等等。

Obviously, fix the names of the modules and subroutines to match yours. 显然,请修改模块和子例程的名称以使其与您的名称相匹配。

EDIT 编辑

Alternatively, you could just call "Close_All" from the original master file. 或者,您可以只从原始主文件中调用“ Close_All”。 Just do the call after the first opening of the code: 只需在代码首次打开后进行调用:

Public Sub masterRun()

Dim wb As Workbook

Set wb = Workbooks.Open("filepath\wb1.xlsm")
'Do whatever you are doing to wb1.

Call CloseAll

End Sub

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

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