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

FolderSub Count
201852
20171005
20161235
20151450
2014900

<tbody>
</tbody>

Thanks in advance for any help
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

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