Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to list all folders and subfolders in Excel?

Have you ever suffered with this problem that list all folders and subfolders from a specified directory into a worksheet? In Excel, there is no a quick and handy way to get the name of all the folders in a specific directory at once. To deal with the task, this article may help you.

List all folders and subfolders with VBA code

Excel Productivity Tools

Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial

Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately.60-day Unlimited Free Trial


arrow blue right bubble List all folders and subfolders with VBA code


If you want to get all the folder names from a specified directory, the following VBA code may help you, please do as this:

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code into the Module Window.

VBA code: List all folders and subfolder names

Sub FolderNames()
'Update 20141027
Application.ScreenUpdating = False
Dim xPath As String
Dim xWs As Worksheet
Dim fso As Object, j As Long, folder1 As Object
With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Choose the folder"
    .Show
End With
On Error Resume Next
xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
Application.Workbooks.Add
Set xWs = Application.ActiveSheet
xWs.Cells(1, 1).Value = xPath
xWs.Cells(2, 1).Resize(1, 5).Value = Array("Path", "Dir", "Name", "Date Created", "Date Last Modified")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.getFolder(xPath)
getSubFolder folder1
xWs.Cells(2, 1).Resize(1, 5).Interior.Color = 65535
xWs.Cells(2, 1).Resize(1, 5).EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
Sub getSubFolder(ByRef prntfld As Object)
Dim SubFolder As Object
Dim subfld As Object
Dim xRow As Long
For Each SubFolder In prntfld.SubFolders
    xRow = Range("A1").End(xlDown).Row + 1
    Cells(xRow, 1).Resize(1, 5).Value = Array(SubFolder.Path, Left(SubFolder.Path, InStrRev(SubFolder.Path, "\")), SubFolder.Name, SubFolder.DateCreated, SubFolder.DateLastModified)
Next SubFolder
For Each subfld In prntfld.SubFolders
    getSubFolder subfld
Next subfld
End Sub

3. Then press F5 key to run this code, and a Choose the folder window will pop out, then you need to select the directory that you want to list the folder and subfolder names, see screenshot:

doc-list-folder-names-1

4. Click OK, and you will get the folder and subfolders path, directory, name, created date and last modified date in a new workbook, see screenshot:

doc-list-folder-names-1


Related article:

How to list files in a directory to worksheet in Excel?


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    jose · 15 days ago
    Great Tool! After long research found this accurate toy :)
  • To post as a guest, your comment is unpublished.
    lloyd · 6 months ago
    Hello. Can you please please help me on a code which I am struggling to find.

    Below are the requirements for the code.



    1. The VBA should go through all the folders and sub-folders
    and check each and every type of file. The user should only give the path for
    the top folder. The code should then check all the folders and sub folders
    within the top folder.



    2. After checking the files, the code should zip all files
    which have not been accessed for more than 3 months. The accessed period is
    something which I should be able to change in future if required. It should
    allow me to change it to 1 month or 5 months if required.



    3. After zipping the files, the code should delete the
    original files which were zipped.



    4. The zipped file should be saved in the same path as the
    original file.
  • To post as a guest, your comment is unpublished.
    tom vincent · 1 years ago
    I modified it to add size:



    Sub FolderNames()
    'Update 20141027
    Application.ScreenUpdating = False
    Dim xPath As String
    Dim xWs As Worksheet
    Dim fso As Object, j As Long, folder1 As Object
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Choose the folder"
    .Show
    End With
    On Error Resume Next
    xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
    Application.Workbooks.Add
    Set xWs = Application.ActiveSheet
    xWs.Cells(1, 1).Value = xPath
    xWs.Cells(2, 1).Resize(1, 6).Value = Array("Path", "Dir", "Name", "Date Created", "Date Last Modified","Size")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder1 = fso.getFolder(xPath)
    getSubFolder folder1
    xWs.Cells(2, 1).Resize(1, 6).Interior.Color = 65535
    xWs.Cells(2, 1).Resize(1, 6).EntireColumn.AutoFit
    Application.ScreenUpdating = True
    End Sub
    Sub getSubFolder(ByRef prntfld As Object)
    Dim SubFolder As Object
    Dim subfld As Object
    Dim xRow As Long
    For Each SubFolder In prntfld.SubFolders
    xRow = Range("A1").End(xlDown).Row + 1
    Cells(xRow, 1).Resize(1, 6).Value = Array(SubFolder.Path, Left(SubFolder.Path, InStrRev(SubFolder.Path, "\")), SubFolder.Name, SubFolder.DateCreated, SubFolder.DateLastModified, SubFolder.Size)
    Next SubFolder
    For Each subfld In prntfld.SubFolders
    getSubFolder subfld
    Next subfld
    End Sub
    • To post as a guest, your comment is unpublished.
      AL · 1 years ago
      When you include the SubFolder.Size function the script no longer list all the subfolders, only the first level.
      How can I include the size and get all subfolders listed?
  • To post as a guest, your comment is unpublished.
    jimbosmiles · 1 years ago
    I am with the others - it works up to a point.

    For me, that point is it creates the new s/s, details the folder I have shown (in Cells A1), the a yellow highlighted bar in row 2 with the headings followed by nothing else!

    The folder I am looking at is empty except for sub folders (i.e. no data files exist) and the sub folders do not appera at all.

    Can anyone help me list the sub folders and their files?
  • To post as a guest, your comment is unpublished.
    Paul J · 2 years ago
    When I run this code it works but it only shows the first folder in side the folder that I choose. For example,

    When I run the code I choose "C:\Users\Johnson\Music" (Note: I have 70 Folders inside my Music Folder)

    When the code runs it only shows the first folder and then list all the folders inside that folder.

    I would like it to list all the folders inside the Music folder.