使用 VBA,如何在不关闭所有工作簿的情况下关闭 Excel 工作簿?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1405742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:59:23  来源:igfitidea点击:

With VBA, how do I close an Excel workbook without closing all workbooks?

excelms-accessvba

提问by PowerUser

Using Access VBA, I want to open an XL file, do stuff, then close it without closing other XL files that are open.

使用 Access VBA,我想打开一个 XL 文件,做一些事情,然后关闭它而不关闭其他打开的 XL 文件。

Option 1: If the last line is "ObjXL.Application.Quit", that closes ALL open Excel files, not just the current one.

选项 1:如果最后一行是“ ObjXL.Application.Quit”,则关闭所有打开的 Excel 文件,而不仅仅是当前的文件。

Option 2: If the last line is "ObjXL.Close", then the workbook closes, but that particular instance of XL stays open (i.e. Excel without a workbook).

选项 2:如果最后一行是“ ObjXL.Close”,则工作簿关闭,但 XL 的特定实例保持打开状态(即,没有工作簿的 Excel)。

So, how do I close an Excel workbook without closing any other workbooks that are open?

那么,如何在不关闭任何其他打开的工作簿的情况下关闭 Excel 工作簿?

Sub x()
    Dim ObjXL As Excel.Workbook
    Set ObjXL = GetObject("C:\Reports\Adhoc Default.xls")
    ObjXL.Application.Visible = True
    ObjXL.Windows(1).Visible = True
    ObjXL.Worksheets(1).Activate
    DoStuff()
    ObjXL.Save
    Option1/2/3?
End Sub

回答by PowerUser

Heh, I answered my own question before anyone else:

呵呵,我先回答了我自己的问题:

Sub xx()
    Dim XLapp As New Excel.Application
    Dim ObjXL As Excel.Workbook
    Set ObjXL = XLapp.Workbooks.Open("C:\reports\adhoc default.xls")
    ObjXL.Application.Visible = True
    ObjXL.Windows(1).Visible = True
    ObjXL.Worksheets(1).Activate
    ObjXL.Save
    ObjXL.Close
    XLapp.Quit
End Sub