Hi Steve,
I too tried to look through a few sites and nothing specific to your problem. Aside from the Application changes (suggested in
#3 , which I suspect you're aware of), the only other difference I read on one site was suggesting include
DoEvents before opening file and after closing file, if the file itself is trying to do anything immediately after opening/before closing.
However, this seems like it will add time/slow your code execution than improve, but if you want to try:
Rich (BB code):
Set wb = Workbooks.Open(FolderPath & Filename, ReadOnly:=True)
DoEvents
wb.Close False
DoEvents
I would also remove the line Set wb = Nothing and place outside of the loop. You don't need to keep clearing the variable inside the loop, given how it's being used.
The link in
#3 suggests the "Why" but as @
Yongle suggests if it's a memory issue caused by one of the files, you could start by opening "groups" of files (e.g. the largest 10%, then the next largest 25% etc) and see if that helps to pinpoint issues with a specific file or not. Alternatively you could use a timer and debug.print to see if you can isolate any files that take longer to open/close than norm:
Rich (BB code):
Dim xTimer As Variant
Do While Filename <> ""
xTimer = Timer
Set wb = Workbooks.Open(FolderPath & Filename, ReadOnly:=True)
DoEvents
wb.Close False
DoEvents
Debug.Print wb.Name & " time: " & Timer - xTimer
Loop
If this code fails to run, you can search for timing code online - I'm including it from memory but may have the syntax wrong.
I'm guessing you're doing more than just opening and closing a file, there may be other optimisations that help but it's just guesswork at this stage! Good luck finding a faster solution
Finally, and I doubt this will make any noticable difference, but you can try using:
Rich (BB code):
Filename = Dir(FolderPath & "*.xls*")
Do While Lenb(Filename)
''your code
Loop