count files in specific folder and display the number into 1 cel

user2151190 picture user2151190 · May 25, 2013 · Viewed 108.5k times · Source

I am trying to know how many files there are in 1 specific folder with .xls extension. I read hundreds of examples with message boxes, but that is not what I'm looking for. I just want to have the number displayed into 1 cel.

Is there someone who likes to help me with that please? I can not post any attempts because I can not get started :-(

best regards, E.

Answer

Santosh picture Santosh · May 26, 2013

Try below code :

Assign the path of the folder to variable FolderPath before running the below code.

Sub sample()

    Dim FolderPath As String, path As String, count As Integer
    FolderPath = "C:\Documents and Settings\Santosh\Desktop"

    path = FolderPath & "\*.xls"

    Filename = Dir(path)

    Do While Filename <> ""
       count = count + 1
        Filename = Dir()
    Loop

    Range("Q8").Value = count
    'MsgBox count & " : files found in folder"
End Sub