简体   繁体   English

使用Excel VBA,如何在第二个工作簿中的“ thisworkbook.close”事件之后使我的原始代码继续执行?

[英]Using Excel VBA, how do I keep my original code executing after a 'thisworkbook.close' event in a 2nd workbook?

In Excel 2007, I have a sheet with a list of other Excel documents, all of which have their own VBA. 在Excel 2007中,我有一个包含其他Excel文档列表的工作表,所有这些文档都有自己的VBA。 My code opens the first workbook on the list, lets its vba run, then when it has completed, marks it as complete, and opens the next workbook in the list. 我的代码打开列表中的第一个工作簿,运行其vba,然后在完成后将其标记为完成,然后打开列表中的下一个工作簿。

All this works fine unless I let one of the other workboks close itself with 'thisworkbook.close'. 除非我让其他工作簿之一使用“ thisworkbook.close”关闭自身,否则所有这些工作都很好。 This stops the VBA running in the original workbook, as well as itself. 这将停止在原始工作簿及其本身中运行的VBA。 If I comment this line out, it all works, but I would rather keep just the master workbook and one sub workbook open at one time. 如果我将这一行注释掉,那么一切都可以,但是我宁愿一次只打开主工作簿和一个子工作簿。

Also, it is unpractical in this case to move all the VBA to the master workbook. 同样,在这种情况下将所有VBA移至主工作簿也是不切实际的。

The code before is a highly simplified version to show the issue: 之前的代码是一个高度简化的版本,用于显示问题:

Workbook1's code: Workbook1的代码:

Sub RunReports()

    Dim wkb1 As Workbook
    Dim wks1 As Worksheet
    Dim lngR As Long
    Dim strReport As String

    Set wkb1 = ThisWorkbook
    Set wks1 = wkb1.Sheets(strDay)

    For lngR = 4 To 1048576
        strReport = wks1.Cells(lngR, 1).Value
        'open the report. Its own VBA will take care of everything else
        Workbooks.Open strReport
        'mark the report as complete
        wks1.Cells(lngR, 2).Value = "done"
    Next lngR
End Sub

the code in the worksheets that are opened: 打开的工作表中的代码:

Private Sub Workbook_Open()

    ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = Now()
    ThisWorkbook.Save
    Application.Wait (Now() + TimeValue("00:00:05"))
    ThisWorkbook.Close

End Sub

If I comment out 'thisworkbook.close', it will open them all, update the time they were opened, and save them. 如果我注释掉“ thisworkbook.close”,它将全部打开它们,更新它们的打开时间,并保存它们。 If not, it does everything up to the first 'thisworkbook.close', closes the first sub workbook, and stops all VBA execution. 如果不是,它将执行直到第一个“ thisworkbook.close”的所有操作,关闭第一个子工作簿,并停止所有VBA执行。

Does anyone have any ideas how to keep the "master" workbook's vba code running after the "sub" workbook's code has finished, when the 'sub' workbook's code contains a 'thisworkbook.close' (edited to make the question clear) 当“子”工作簿的代码包含“ thisworkbook.close”时,是否有人有任何想法如何在“子”工作簿的代码完成后保持“主”工作簿的vba代码运行(已编辑以使问题更清楚)

Use standard COM ways of doing things. 使用标准的COM处理方式。 Take a reference to each workbook (not excel.application) using GetObject(filename) . 使用GetObject(filename)引用每个工作簿(不是excel.application GetObject(filename) Do what you want then don't close it in sub book, but set the reference to nothing in your master (which happens when you do set exceldoc = nothing or reach an End Function/Sub ). 做您想做的事,然后不要在子书中关闭它,而是在母版中将引用set exceldoc = nothing (这在您set exceldoc = nothing或到达End Function/Sub )。 Don't do both as that's voodoo programming. 两者都不做,因为这是伏都教编程。

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

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