VBA Code to list All the Folders and Sub Folders inside a given Main Folder Path<\/h2>\n\r\nSub ListAllFoldersAndSubFolders(SourceFolderName As String, isSubFolder As Boolean)\r\n\r\n'--- For Example:Folder Name= \"C:\\Folder Name\\\" and IsSubfolder Flag as True or false\r\n\r\nDim FSO As Scripting.FileSystemObject\r\nDim SourceFolder As Scripting.folder, SubFolder As Scripting.folder\r\nDim FileItem As Scripting.File\r\n\r\nOn Error GoTo err\r\n \r\n Set FSO = New Scripting.FileSystemObject\r\n Set SourceFolder = FSO.GetFolder(SourceFolderName)\r\n\r\n For Each SubFolder In SourceFolder.Subfolders\r\n Cells(strtRow, strtCol).Value = SubFolder.Name\r\n strtRow = strtRow + 1\r\n If isSubFolder = True Then\r\n strtCol = strtCol + 1\r\n ListAllFoldersAndSubFolders SubFolder.Path, isSubFolder\r\n End If\r\n Next SubFolder\r\n ' At the end of the subfolder\r\n ' set the column back to the\r\n ' immediate main folder backward\r\n strtCol = strtCol - 1\r\nerr:\r\n If err.Number <> 0 Then MsgBox err.Description\r\nEnd Sub\r\n<\/code><\/pre>\nHow to call above function or How to use above <\/h2>\n 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. \nTherefore you need to call this function as shown in the below example:<\/p>\n
\r\nPublic strtRow As Integer\r\nPublic strtCol As Integer\r\nSub CallAboveFunction()\r\n 'set the start row and column for displaying the list\r\n strtRow = 2\r\n strtCol = 2\r\n ListAllFoldersAndSubFolders \"C:\\Users\\vmishra\\Documents\\Davinci\\VISHWA\", False\r\nEnd Sub\r\n<\/code><\/pre>\nFREE Download <\/h2>\n 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.<\/p>\n
\n \n<\/a> <\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"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, […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246,1675],"tags":[],"class_list":["post-4768","post","type-post","status-publish","format-standard","hentry","category-macro","category-excel-macro-for-beginners"],"yoast_head":"\n
List all Folders and Sub-folders in Hierarchical Structure [FREE DOWNLOAD] - Let's excel in Excel<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n