Count Files in Folder

proctk

Well-known Member
Joined
Dec 24, 2004
Messages
840
Hi

I'm trying to find code that will count the number of excel file in a folder called Viewpoint and place the result in cell b3 on a worksheet called Sum any help is great

thank you
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Something like this?:

Code:
Sub listFiles()
Dim filesToProcess As Long
Dim myPath As String

    myPath = "C:\ViewPoint\"

    'Use the Filesearch Object
    With Application.FileSearch
        .NewSearch
        .LookIn = myPath
        .SearchSubFolders = False
        .Filename = "*.*"
        .Execute
        Worksheets("Sum").Range("B3").Value = .FoundFiles.Count
    End With
        
End Sub

Hope that helps!
 
Upvote 0
Though I think Taz's solution is slicker I thought I might as well post this alternative , since I had it kicking around in my soltion library. :wink:

Code:
Public Sub Demo()
    MsgBox CountFiles("C:\ViewPoint\", "*.xls")
End Sub

Function CountFiles(tgtDir As String, Extention As String)
    Dim fName As String
    Dim cnt As Integer
    
    fName = Dir(tgtDir)
    cnt = 0
   
    Do While fName <> ""
       
        If fName <> "." And _
        fName <> ".." And _
        fName Like Extention Then
            cnt = cnt + 1
        End If
        
        fName = Dir()
    Loop
   
    CountFiles = cnt
End Function
 
Upvote 0
Or Alternatively

Code:
Sub FileCount()
    Dim fsoObj As Object  '// Scripting.FileSystemObject
    Dim fsoMapp As Object '// Scripting.Folder
   
    Set fsoObj = CreateObject("Scripting.FileSystemObject")
    Set fsoMapp = fsoObj.GetFolder("C:\ExcelFiles\Useful")
    
    MsgBox fsoMapp.Files.Count
    
    Set fsoObj = Nothing
    Set fsoMapp = Nothing
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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