0

I want to count the filenames in (sub-)folders, based on a string and path in Excel. It works nicely with the comprehensible vba-code below, except that it isn't searching in the subfolders. Just adding a wildcard in in the path gives an error. I've spent a couple of hours on it, but although it seems pretty easy, I just cannot get it to work.

Question Can anyone give me a pointer how this code does a countsearch in the subfolders as well?

Code

Sub CountFiles()

Sheets("Kex").Activate

Dim i As Integer
Dim x As Integer
Dim Folder As String
Dim ExcelFN As String
Dim NumFiles As Integer

For i = 1 To 400
    NumFiles = 0 '<-- Reset count

    oFolder = Sheets("Kex").Range("B" & i).Value & "\"
    ExcelFN = Sheets("Kex").Range("A" & i).Value

    FileName = Dir(oFolder & ExcelFN & "*" & ".jpg")

    While FileName <> ""
        NumFiles = NumFiles + 1
        FileName = Dir()
    Wend

    Sheets("Kex").Range("C" & i) = NumFiles
Next i

End Sub
Egan Wolf
  • 3,533
  • 1
  • 14
  • 29
Marco
  • 11
  • 2
  • Please check https://stackoverflow.com/a/38406605/7889129 – Maddy Jun 22 '17 at 06:12
  • Thx Maddy, that does the trick indeed! https://stackoverflow.com/questions/38391938/count-how-many-of-files-in-each-folder-and-subfolder-then-display-separately/38406605#38406605 counts all files though and I don't see how to modify the code so it does only count the given filenames in column A. – Marco Jun 22 '17 at 08:08

0 Answers0