I threw together the two methods I use to open files in a folder based on criteria to see which runs quicker. (In this case it was m1 by roughly 1 second per file). I was wondering if there are any other accepted methods of opening files en masse.
p is the folderpath and p2 is a filter such as "*.xlsx" or "20120101-*"
p is the folderpath and p2 is a filter such as "*.xlsx" or "20120101-*"
Code:
Const p As String = "FOLDERPATH"
Const p2 As String = "FILEFILTER"
Dim i As Integer
Dim t As Date
Dim wb As Workbook
Sub m1()
Dim a As String
t = Now
Application.ScreenUpdating = False
a = Dir(p & p2)
i = 1
While a <> vbNullString
Application.DisplayAlerts = False
Set wb = Workbooks.Open(p & a, False, True)
Debug.Print i; ","; wb.FullName; ","; ((Now - t) * 86400)
wb.Close False
Application.DisplayAlerts = True
a = Dir
i = i + 1
Wend
Application.ScreenUpdating = True
Debug.Print "m1 "; ((Now - t) * 86400)
End Sub
Sub m2()
Dim o As Object, o2 As Object
t = Now
Application.ScreenUpdating = False
Set o = CreateObject("scripting.filesystemobject").getfolder(p)
i = 1
For Each o2 In o.Files
If o2.Name Like p2 Then
Application.DisplayAlerts = False
Set wb = GetObject(o2.Path)
Debug.Print i; ","; wb.FullName; ","; ((Now - t) * 86400)
wb.Close False
Application.DisplayAlerts = True
End If
i = i + 1
Next o2
Application.ScreenUpdating = True
Debug.Print "m2 "; ((Now - t) * 86400)
End Sub