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