Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Excel file for reading with VBA without display

Tags:

file-io

excel

vba

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?

like image 708
notnot Avatar asked Feb 23 '09 23:02

notnot


People also ask

How do I run VBA code in Excel without opening it?

You can't run a Excel VBA Macro without opening the File that contains the macro. If you want you can launch the excel application in hidden mode and then run the macro after opening the file in hidden mode from a VBS file.

How do I run a macro without showing steps?

To hide the actions of the macro from the screen, add Application. ScreenUpdating = False to the beginning of your code. Set it back to True at the end.


2 Answers

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

like image 65
Patrick McDonald Avatar answered Sep 19 '22 00:09

Patrick McDonald


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.

like image 24
iDevlop Avatar answered Sep 22 '22 00:09

iDevlop