Quickest way to handle lots of files

jaybee3

Active Member
Joined
Jun 28, 2010
Messages
307
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-*"

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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,215,360
Messages
6,124,492
Members
449,166
Latest member
hokjock

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top