使用VBA打开Excel文件,无需显示

我想用macros来search现有的Excel文件,但是我不想在代码打开时显示这些文件。 有没有办法让他们打开“在后台”,可以这么说呢?

不知道你是否可以在当前的Excel实例中隐形打开它们

您可以打开一个新的Excel实例,然后隐藏它,然后打开工作簿

Dim app as New Excel.Application app.Visible = False 'Visible is False by default, so this isn't necessary Dim book As Excel.Workbook Set book = app.Workbooks.Add(fileName) ' ' Do what you have to do ' book.Close SaveChanges:=False app.Quit Set app = Nothing 

正如其他人所发布的,确保在完成任何打开的工作簿后清理

如果这符合您的需求,我会简单地使用

 Application.ScreenUpdating = False 

还有加速代码的好处,而不是通过使用第二个Excel实例来减慢速度。

即使你已经得到了答案,对于那些发现这个问题的人来说,也可以打开Excel电子表格作为JET数据存储。 从项目中借用连接string,它会看起来像这样:

 strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & objFile.Path & ";Extended Properties=""Excel 8.0;HDR=Yes""" strSQL = "SELECT * FROM [RegistrationList$] ORDER BY DateToRegister DESC" 

请注意,“RegistrationList”是工作簿中选项卡的名称。 有几个教程漂浮在networking上,你可以或不可以通过这种方式访问​​工作表的细节。

只是想我会补充。 🙂

要将现有Excel实例中隐藏的工作簿打开,请使用以下命令:

  Application.ScreenUpdating = False Workbooks.Open Filename:=FilePath, UpdateLinks:=True, ReadOnly:=True ActiveWindow.Visible = False ThisWorkbook.Activate Application.ScreenUpdating = True 

从Excel的新实例中打开它们。

 Sub Test() Dim xl As Excel.Application Set xl = CreateObject("Excel.Application") Dim w As Workbook Set w = xl.Workbooks.Add() MsgBox "Not visible yet..." xl.Visible = True w.Close False Set xl = Nothing End Sub 

你需要记得清理完成后。

以隐藏的方式打开工作簿,然后将其设置为“已保存”,以便用户在closures时不会收到提示。

 Dim w As Workbooks Private Sub Workbook_Open() Application.ScreenUpdating = False Set w = Workbooks w.Open Filename:="\\server\PriceList.xlsx", UpdateLinks:=False, ReadOnly:=True 'this is the data file were going to be opening ActiveWindow.Visible = False ThisWorkbook.Activate Application.ScreenUpdating = True End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) w.Item(2).Saved = True 'this will suppress the safe prompt for the data file only End Sub 

这是Ashok发布的答案的一些衍生物。

通过这样做,尽pipe不会提示您将更改保存回Excel文件中。 如果您将Excel中的文件作为validation的数据源,这非常棒。 例如,如果工作簿包含产品名称和价格数据,则可以将其隐藏,并且可以显示一个Excel文件,该文件表示包含从该价目表validation的产品的下拉式发票。

然后,您可以将价目表存储在某个networking上的共享位置,并使其成为只读。

iDevlop和Ashok的答案的问题是,根本问题是一个Exceldevise缺陷(显然),其中Open方法无法尊重Application.ScreenUpdating设置False。 因此,将其设置为False对此问题没有任何好处。

如果Patrick McDonald的解决scheme由于启动Excel的第二个实例而导致负担过重,那么我发现的最佳解决scheme是尽可能快地重新激活原始窗口,以最小化打开的工作簿可见的时间:

 Dim TempWkBk As Workbook Dim CurrentWin As Window Set CurrentWin = ActiveWindow Set TempWkBk = Workbooks.Open(SomeFilePath) CurrentWin.Activate 'Allows only a VERY brief flash of the opened workbook TempWkBk.Windows(1).Visible = False 'Only necessary if you also need to prevent 'the user from manually accessing the opened 'workbook before it is closed. 'Operate on the new workbook, which is not visible to the user, then close it... 

一个更简单的方法,不涉及操作活动窗口:

 Dim wb As Workbook Set wb = Workbooks.Open("workbook.xlsx") wb.Windows(1).Visible = False 

从我可以告诉Windows索引工作簿应该始终是1 。 如果有人知道任何会使这个不真实的竞争条件,请让我知道。

在Excel中,隐藏工作簿,并将其保存为隐藏。 当你的应用程序加载它们时,将不会显示。

编辑:重新阅读后,很明显,这些工作簿不是你的应用程序的一部分。 这样的解决scheme将不适合用户工作簿。