打开 Excel 文件以使用 VBA 读取而不显示
问题描述:
我想用宏搜索现有的 Excel 文件,但我不想在代码打开这些文件时显示这些文件.有没有办法让它们在后台"打开,可以这么说?
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?
答
不确定是否可以在当前excel实例中隐形打开
Not sure if you can open them invisibly in the current excel instance
不过,您可以打开一个新的 excel 实例,将其隐藏,然后再打开工作簿
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