Looking for some help to make this code suit my needs.
Goal is to list the number of files in several folders. The folder names are listed in column C and always hosted under "C:\Users\diego\test".
The number of files should be displayed in column K.
Below code works, but it ignores folders with spaces (e.g: "test folder"). Also, would be awesome if users could change the path of those folders without editing the VBA code.
Goal is to list the number of files in several folders. The folder names are listed in column C and always hosted under "C:\Users\diego\test".
The number of files should be displayed in column K.
Below code works, but it ignores folders with spaces (e.g: "test folder"). Also, would be awesome if users could change the path of those folders without editing the VBA code.
Code:
Sub Count_files()
Dim files As Variant
Set files = CreateObject("System.Collections.ArrayList")
Call GetFiles("C:\Users\diego\test", files)
Dim searchText As String
Dim fileCount As Integer
For r = 2 To Rows.Count
searchText = LCase(Cells(r, 3))
'If (searchText & "" = "") Then
If (searchText = vbNullString) Then
Exit For
End If
fileCount = 0
For Each f In files
If InStr(f, searchText) > 0 Then
fileCount = fileCount + 1
End If
Next
Cells(r, 11) = fileCount
Next r
End Sub
Sub GetFiles(sFolderName As String, vFiles As Variant)
Set oFSO = New Scripting.FileSystemObject
Set oFolder = oFSO.GetFolder(sFolderName)
For Each oFileItem In oFolder.files
vFiles.Add LCase(oFileItem.Name)
Next oFileItem
For Each oSubFolder In oFolder.SubFolders
Call GetFiles(oSubFolder.Path, vFiles)
Next oSubFolder
End Sub
Last edited: