简体   繁体   English

Excel VBA 关闭工作簿并触发新宏

[英]Excel VBA Close Workbook and Trigger New Macro

I've been tinkering with this for hours and I can't figure it out.我一直在修补这个好几个小时,但我无法弄清楚。 I've got a main workbook that launches other macro-enabled templates (which generate and save outputs) and then attaches the outputs to an email.我有一个主工作簿,它启动其他启用宏的模板(生成和保存输出),然后将输出附加到电子邮件。 (I can't combine them all into one workbook because the number of tabs, macros, and size would become far too cumbersome). (我不能将它们全部合并到一个工作簿中,因为选项卡、宏和大小的数量会变得太麻烦)。 Due to the confidential nature of some of the things I'm working with, I can't share my exact code - but I've trimmed down/replaced values and provided "examples" of the code I've tried below.由于我正在处理的某些事情的机密性,我无法分享我的确切代码 - 但我已经削减/替换了值并提供了我在下面尝试过的代码的“示例”。

The issue I'm having is in trying to accomplish the following:我遇到的问题是试图完成以下任务:

  1. MainWB opens SubWB1 and calls (Application.Run) a macro MainWB 打开 SubWB1 并调用 (Application.Run) 一个宏
  2. Macro in SubWB1 generates and saves an excel output file SubWB1中的宏生成并保存excel输出文件
  3. SubWB1 closes and MainWB continues with next report SubWB1 关闭,MainWB 继续下一份报告

My problem is that, once I close the SubWB1, the macros within the MainWB stop running... I've tried:我的问题是,一旦我关闭 SubWB1,MainWB 中的宏就会停止运行……我试过:

1. Try 1: Calling the SubWB1 macro in the middle of a MainWB macro with the continuation steps after: 1. 尝试 1:在 MainWB 宏中间调用 SubWB1 宏,并执行以下后续步骤:

    Sub AfternoonReport () 'macro in MainWB
        'do stuff
        Workbooks.Open FileName:="C:\Users\me\Documents\DailyReport\submacro1.xlsm"
        Application.Run "'submacro1.xslm'!triggerFromMain" 'macro ends with .close, save changes false
      '************STOPS HERE***********
        Application.Wait Now + TimeValue("00:00:03")
        generateEmail 'macro within MainWB
    End Sub

2. Try 2: Calling a continuation sub within the MainWB at the end of the macro in the subWB: 2.尝试2:在subWB中宏的末尾调用MainWB中的continuation sub:

    Sub AfternoonReport () 'macro in MainWB
        'do stuff
        Workbooks.Open FileName:="C:\Users\me\Documents\DailyReport\submacro1.xlsm"
        Application.Run "'submacro1.xslm'!triggerFromMain"
    End Sub

    Sub triggerFromMain () 'macro in subWB
        'do stuff
        Application.Run "'mainWB.xslm'!continueFromSub1"
    End Sub

    Sub continueFromSub1 () 'macro in MainWB
        'do stuff
        Workbooks("submacro1.xlsm").Close SaveChanges:=False
      '************STOPS HERE***********
        Application.Wait Now + TimeValue("00:00:03")
        generateEmail 'macro within MainWB
    End Sub

3. Try 3: The same example above, but assigning the subWB to a public workbook object in the mainWB: 3.尝试3:与上面相同的示例,但将subWB分配给mainWB中的公共工作簿对象:

    'Declarations in mainWB
    Public subWBobj As Workbook

    Sub AfternoonReport () 'macro in MainWB
        'do stuff
        Set subWBobj = Workbooks.Open FileName:="C:\Users\me\Documents\DailyReport\submacro1.xlsm"
        Application.Run "'submacro1.xslm'!triggerFromMain"
    End Sub

    Sub triggerFromMain () 'macro in subWB
        'do stuff
        Application.Run "'mainWB.xslm'!continueFromSub1"
    End Sub

    Sub continueFromSub1 () 'macro in MainWB
        'do stuff
        subWBobj.Close SaveChanges:=False
      '************STOPS HERE***********
        Application.Wait Now + TimeValue("00:00:03")
        generateEmail 'macro within MainWB
    End Sub

4. Try 4: Calling the continuation macro from the BeforeClose event in the subWB 4.尝试4:从subWB中的BeforeClose事件调用continuation宏

    Sub AfternoonReport () 'macro in MainWB
        'do stuff
        Workbooks.Open FileName:="C:\Users\me\Documents\DailyReport\submacro1.xlsm"
        Application.Run "'submacro1.xslm'!triggerFromMain"
    End Sub

    Sub triggerFromMain () 'macro in subWB
        'do stuff
        ThisWorkbook.Close SaveChanges:=False
    End Sub

    Sub Workbook_BeforeClose () 'macro in subWB
        Application.Run "'mainWB.xslm'!continueFromSub1"
      '********Doesnt allow the workbook to close and just fires macros in the mainWB - causing other issues*******
    End Sub

Note: In all but example 4 above, I don't receive any errors - so the code isn't breaking, it just seems to be exiting the macro in the MainWB on the ".Close" function, and then therefore isn't continuing with any further code.注意:除了上面的示例 4 之外,我没有收到任何错误 - 所以代码没有中断,它似乎只是在“.Close”函数上退出 MainWB 中的宏,因此不是继续任何进一步的代码。

Any suggestions would be greatly appreciated!任何建议将不胜感激! I've tried everything I can think of and I can only stare at the same lines of code for so long!我已经尝试了所有我能想到的方法,但我只能盯着同一行代码看了这么久! :) :)

The part that's stopping your macro is the .Close statement in the subWB.停止宏的部分是 subWB 中的.Close语句。 Remove that line from the subWB and close that workbook from the main:从 subWB 中删除该行并从 main 中关闭该工作簿:

Option Explicit

Sub AfternoonReport()
    Dim msg As String
    msg = "Macro executed at " & Format(Now(), "dd-mmm-yyyy hh:mm")
    Debug.Print "from mainWB: " & msg

    Dim otherWB As Workbook
    Set otherWB = Workbooks.Open(Filename:="C:\Temp\submacro1.xlsm")
    Application.Run "'submacro1.xlsm'!triggerFromMain" 'macro ends with .close, save changes false

    Debug.Print "back from the sub macro and we're done."

    otherWB.Close SaveChanges:=False
End Sub

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

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