【VBA】Get folder count in folder

You can get  “folder count in folder“!

PR

VBA Code

As an example, get folder count in “test” folder.

“test” folder
“test” folder
There are 5 folders in the folder.
There are 5 folders in the folder.
Option Explicit

Sub sample()
    
    Dim targetFolder As String
    Dim fso As Object
    Dim folderCount As Long

    targetFolder = "C:\Users\user\Desktop\test"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Get folder count in folder
    folderCount = fso.GetFolder(targetFolder).SubFolders.Count
    
    MsgBox ("folder count : " & folderCount)
    
    Set fso = Nothing
    
End Sub

Get folder count in folder(Line 14).

PR

Result

You can get  “folder count in folder“!

Result
PR

FYI No.1

Also, you can get “file count in folder“!

For more information, see the following article:

PR

FYI No.2

For more information about following, see the following official online manual:

GetFolder method” of “FileSystemObject”


SubFolders property” of “Folder Object”


Folders collection


Count property” of “collection”

タイトルとURLをコピーしました