List all Folders and Sub-folders in Hierarchical Structure [FREE DOWNLOAD]

.

Dear Readers,

Many of you were looking for a excel tool same as File Manager – which lists down all the files from each and every folders and subfolders, to list down all the folders and sub-folders under a given Main Folder Path. Therefore rather than replying to you all individually a piece of code, I though of posting an article with a downloadable file which can be used to list all folders and subfolders in your excel sheet in a Hierarchical structure (same as the folders and sub-folders are structured).

List Folders and Sub Folders

List Folders and Sub Folders

VBA Code to list All the Folders and Sub Folders inside a given Main Folder Path

Sub ListAllFoldersAndSubFolders(SourceFolderName As String, isSubFolder As Boolean)

'--- For Example:Folder Name= "C:\Folder Name\" and IsSubfolder Flag as True or false

Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
Dim FileItem As Scripting.File

On Error GoTo err
    
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

        For Each SubFolder In SourceFolder.Subfolders
            Cells(strtRow, strtCol).Value = SubFolder.Name
            strtRow = strtRow + 1
            If isSubFolder = True Then
                strtCol = strtCol + 1
                ListAllFoldersAndSubFolders SubFolder.Path, isSubFolder
            End If
        Next SubFolder
        ' At the end of the subfolder
        ' set the column back to the
        ' immediate main folder backward
        strtCol = strtCol - 1
err:
 If err.Number <> 0 Then MsgBox err.Description
End Sub

How to call above function or How to use above

As you can see to run above function it requires certain parameter like Main Folder Path and a Boolean flag to say if you want to list all subfolders as well or just the folders which are there under the main folder.
Therefore you need to call this function as shown in the below example:


Public strtRow As Integer
Public strtCol As Integer
Sub CallAboveFunction()
    'set the start row and column for displaying the list
    strtRow = 2
    strtCol = 2
    ListAllFoldersAndSubFolders "C:\Users\vmishra\Documents\Davinci\VISHWA", False
End Sub

FREE Download

Using the above functions and with some formatting, I have created a simple excel tool where you can list all your folders and sub-folders in a hierarchical structure. You can download this file, use it, refer to the code, share it with friends to help. In short you can do anything you want with this excel :D.


Download Now

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

5 Comments

  1. P.Vijay

    I cannot thank you enough for this!

    Reply
    • Vishwamitra Mishra

      Thanks for stopping by Vijay. I am glad that it helped you.

      Reply
  2. farhan

    Sir,

    Good Morning

    my name is farhan , i start learning VBA and i am very interested if you can teach me by tutorial or step by step that how you did this ..
    it will clear my concepts and help alot
    i was trying to copy the code and paste in module of my excel file
    but it wont work ,
    i wana learn sir

    Reply
  3. Ahmed

    Very useful, is there any addition to list the files name of each folder

    Reply
  4. Tartarugo

    Hi and congratulations for this wonderful and very useful work. Could I kindly ask you what would need to be done to add a hyperlink to all folders, subfolders and files names found? So it would be just perfect for me. I’m going crazy trying to do this but I don’t have a level that would allow me to do this. Thank you very much for your kindly help!!!

    Reply

Trackbacks/Pingbacks

  1. Excel VBA Code to List files from Folder and Sub-folders in Excel Workbook - […] List all Folders and Sub-folders in Hierarchical Structure [FREE DOWNLOAD] How to create folders in windows via Excel VBA…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest