简体   繁体   English

VBA-在打开的工作簿上执行宏

[英]VBA - Execute Macro on Workbook Open

I have 3 xlsm workbooks with a bunch of macros inside of them.. When each one is opened, the user is presented with a pop up that asks if they want to execute the Call_All macro which would run them all. 我有3个xlsm工作簿,其中包含一堆宏。.打开每个宏时,会向用户显示一个弹出窗口,询问他们是否要执行将全部运行的Call_All宏。 If the user clicks yes, then it executes, and if no then nothing happens. 如果用户单击“是”,它将执行,如果没有,则什么也不会发生。

The reason I have done it this way, is because I am trying to automate the admin work as far as possible and make the process as "dummy proof" as I can for the users. 之所以这样做,是因为我试图使管理工作尽可能自动化,并使该过程成为用户的“虚拟样机”。 So if I don't put the pop up to ask the user what they want to do and I simply call the Call_All macro in the "this workbook" file, then whenever the file is opened - even if it is to modify something (such as code), the entire macro will execute. 因此,如果我不弹出窗口询问用户他们想做什么,而我只是在“此工作簿”文件中调用Call_All宏,那么只要打开文件,即使要修改某些内容(例如作为代码),整个宏就会执行。

Now, the issue I am having is this: 现在,我遇到的问题是:

I run a series of scripts and the very last script opens up the first workbook with the "Yes/No" pop up. 我运行了一系列脚本,最后一个脚本打开了第一个工作簿,并弹出“是/否”。 When the user clicks on yes for this workbook, the macro's are executed perfectly. 当用户单击此工作簿的“是”时,将完美执行宏。 The very last macro in each workbook, opens up the next xlsm file, upon which the user is asked again if they want to execute the macros in the new workbook. 每个工作簿中的最后一个宏将打开下一个xlsm文件,在该文件上再次询问用户是否要执行新工作簿中的宏。 However, this time it does not execute and absolutely nothing happens. 但是,这一次它没有执行,绝对没有任何反应。 The very same thing happens again when the next xlsm file is opened. 当打开下一个xlsm文件时,同样的事情再次发生。

Note: If I open each xlsm file individually and click yes, then they work fine. 注意:如果我分别打开每个xlsm文件并单击“是”,则它们可以正常工作。 It is only when I open the file from the previous workbook. 仅当我打开上一个工作簿中的文件时。

Here is the code to call the Call_All macro (This is in the "ThisWorkbook" File): 这是调用Call_All宏的代码(位于“ ThisWorkbook”文件中):

Private Sub Workbook_Open()

    Dim varResponse As Variant

    varResponse = MsgBox("Run Macro?", vbYesNo, "Selection")
    If varResponse <> vbYes Then

        Exit Sub

    Else

        Call_All

    End If



End Sub

The code is exactly the same in all 3 workbooks. 代码在所有3个工作簿中都完全相同。

. Look at this resource (about half way down the page: https://exceloffthegrid.com/how-to-run-a-macro-from-another-workbook/ 查看此资源(大约在页面的中间: https : //exceloffthegrid.com/how-to-run-a-macro-from-another-workbook/

Sub CallAnotherMacro()

Application.Run "'Another Workbook.xlsm'!NameOfMacro"

End Sub

Answered by @Rory in a separate post. @Rory在另一篇帖子中回答。 I continued to receive the same issue even with @Jerry's answer above. 即使上面有@Jerry的回答,我仍然收到相同的问题。

This was the reason: 这是原因:

Closing the first workbook that called the code in the second one. 关闭第一个调用第二个代码的工作簿。

@Rory said: @罗里说:

Then I suspect that is your issue - that code is the caller of the code you're currently running. 然后我怀疑这是您的问题-该代码是您当前正在运行的代码的调用方。 If you end that, you end the called code too. 如果结束该操作,那么也将结束被调用的代码。 I suggest you try using Application.OnTime to call the code in the second workbook so that the first code can actually finish before the workbook is closed. 我建议您尝试使用Application.OnTime来调用第二个工作簿中的代码,以便第一个代码实际上可以在关闭工作簿之前完成。

This I could not unfortunately get @Rory's suggestion for Application.OnTime to work either, however, I found the below link which explains why: 不幸的是,我还是无法得到@Rory对Application.OnTime的建议,但是,我找到了下面的链接来解释原因:

Closing a workbook from an other one without stopping code 在不停止代码的情况下从另一个关闭工作簿

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

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