Speed up the process

ScotTFO

Board Regular
Joined
May 30, 2008
Messages
72
I am currently using this code which does work, but it's SLOW...

I was wondering if there was an alternative method or a way I could speed up this code.

It's searching a directory and it's sub-directories for files that are either xls or doc then counting them all up.

Code:
    With Application.FileSearch
        .LookIn = strSourceFolder
        .FileType = msoFileTypeAllFiles
        .searchSubFolders = True
        .Execute
         
        For x = 1 To .FoundFiles.Count
            i = x
            On Error GoTo Skip
             
            Set objFile = objFSO.GetFile(.FoundFiles(x)) 
            If Right(objFile.Name, 3) = "xls" Or Right(objFile.Name, 3) = "doc" Then
                PopulateDirectoryList = PopulateDirectoryList + 1
            End If
            
Skip:
        Next x
         
    End With

Thanks!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Perhaps this may be faster...

Code:
Sub Test()
Dim strSourceFolder As String
Dim x As Long
Dim PopulateDirectoryList As Long
Dim MyFileTypes As Variant
 
MyFileTypes = Array("*.xls", "*.doc")
strSourceFolder = "C:\Path"
 
For x = LBound(MyFileTypes) To UBound(MyFileTypes)
    With Application.FileSearch
        .LookIn = strSourceFolder
        .FileType = msoFileTypeAllFiles
        .SearchSubFolders = True
        .Filename = MyFileTypes(x)
        .Execute
    PopulateDirectoryList = PopulateDirectoryList + .FoundFiles.count
    End With
Next x
    
MsgBox PopulateDirectoryList
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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