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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
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

Nimrod

MrExcel MVP
Joined
Apr 29, 2002
Messages
6,259
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

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
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,195,674
Messages
6,011,098
Members
441,582
Latest member
Topkapi

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