I found code on YouTube.
It counts Excel files.
How can it read all the extensions in a folder?It counts one directory.
How can it read the subfolders and count files in them as well?It displays the count answer in a message box.
How can it display the answer in Column B?
E.g. There are five subfolders and each contains files with different extensions.
The code should read all the subfolders and list the name of folder in Excel and also count and return the answer in front of each folder name.
Sub CountFiles()
Dim strDir As String
Dim fso As Object
Dim objFiles As Object
Dim obj As Object
Dim lngFileCount As Long
strDir = "E:\2022\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strDir).Files
lngFileCount = objFiles.count
MsgBox lngFileCount 'Total number of files
'***************************************************
'NOTE: Ensure that the following code does not overwrite _
anything in your workbook.
'Active worksheet should be a blank worksheet
For Each obj In objFiles
ActiveSheet.Cells(Rows.count, "A").End(xlUp).Offset(1, 0) = obj.Name
Next obj
Set objFiles = Nothing
Set fso = Nothing
Set obj = Nothing
End Sub

List Subfolders