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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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,191,383
Messages
5,986,302
Members
440,017
Latest member
vasanrajeswaran

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
Top