Macro to List folder and count sub-folder one level down

cjouban

New Member
Joined
Jan 21, 2014
Messages
23
I have searched and cannot find a way of doing this. I need to list the folders and count the subfolders within that folder, but stop there. I don't want to count the sub-folders sub-folder.

An output like this would be great

[TABLE="width: 168"]
<tbody>[TR]
[TD]Folder[/TD]
[TD]Sub Count[/TD]
[/TR]
[TR]
[TD]2018[/TD]
[TD]52[/TD]
[/TR]
[TR]
[TD]2017[/TD]
[TD]1005[/TD]
[/TR]
[TR]
[TD]2016[/TD]
[TD]1235[/TD]
[/TR]
[TR]
[TD]2015[/TD]
[TD]1450[/TD]
[/TR]
[TR]
[TD]2014[/TD]
[TD]900[/TD]
[/TR]
</tbody>[/TABLE]

Thanks in advance for any help
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this macro. Change the startFolderPath to the full path of the folder containing the 2018, 2017, etc. folders.

Code:
Public Sub Count_Subfolders()
    
    Dim startFolderPath As String
    Dim FSO As Object
    Dim startFolder As Object, subfolder As Object
    Dim r As Long
    
    startFolderPath = "C:\path\to\start\folder"
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set startFolder = FSO.GetFolder(startFolderPath)
    
    With ActiveSheet
        .Cells.ClearContents
        .Range("A1:B1").Value = Array("Folder", "Subfolder count")
        r = 1
        For Each subfolder In startFolder.SubFolders
            r = r + 1
            .Cells(r, 1).Value = subfolder.Name
            .Cells(r, 2).Value = subfolder.SubFolders.Count
        Next
    End With
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top