Open Excel file for reading with VBA without display
I want to search through existing Excel files with a macro, but I don't want to display those files when they're opened by the code. Is there a way to have them open "in the background", so to speak?
Not sure if you can open them invisibly in the current excel instance
You can open a new instance of excel though, hide it and then open the workbooks
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
As others have posted, make sure you clean up after you are finished with any opened workbooks
Read more... Read less...
If that suits your needs, I would simply use
Application.ScreenUpdating = False
with the added benefit of accelerating your code, instead of slowing it down by using a second instance of Excel.
To open a workbook as hidden in the existing instance of Excel, use following:
Application.ScreenUpdating = False Workbooks.Open Filename:=FilePath, UpdateLinks:=True, ReadOnly:=True ActiveWindow.Visible = False ThisWorkbook.Activate Application.ScreenUpdating = True
Even though you've got your answer, for those that find this question, it is also possible to open an Excel spreadsheet as a JET data store. Borrowing the connection string from a project I've used it on, it will look kinda like this:
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"
Note that "RegistrationList" is the name of the tab in the workbook. There are a few tutorials floating around on the web with the particulars of what you can and can't do accessing a sheet this way.
Just thought I'd add. :)
A much simpler approach that doesn't involve manipulating active windows:
Dim wb As Workbook Set wb = Workbooks.Open("workbook.xlsx") wb.Windows(1).Visible = False
From what I can tell the Windows index on the workbook should always be
1. If anyone knows of any race conditions that would make this untrue please let me know.
The problem with both iDevlop's and Ashok's answers is that the fundamental problem is an Excel design flaw (apparently) in which the Open method fails to respect the Application.ScreenUpdating setting of False. Consequently, setting it to False is of no benefit to this problem.
If Patrick McDonald's solution is too burdensome due to the overhead of starting a second instance of Excel, then the best solution I've found is to minimize the time that the opened workbook is visible by re-activating the original window as quickly as possible:
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...