VBA to List all Folders, Subfolders and files in a directory

Jindalga2

New Member
Joined
Feb 17, 2024
Messages
4
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello All,

I am trying to create a VBA in Excel that replicates a cmd script to list out contents of a folder, subfolders and files, and found this piece of code from an earlier post dated Dec 13, 2018:

The code in the link works perfectly fine, but has one minor short coming.
Each time we try to browse through the contents of a folder, we need to copy the folder path, go to the code module manually and and then paste the same in the code.

Was just wondering if there is a way if this process could be automated either by usung Folder Picker dialogue box [ Application.FileDialog(msoFileDialogFolderPicker) ] or copying the address of the folder and pasting it in a particular cell (example A2) and then automating the code to fetch the path from the designated cell (A2, as in the example), and display the contents.

I am a novice in the field of excel VBA and tried my hand to make some changes to the existing code, but that did not materialise.
Your assistance in this regard shall be of immense help!

Thanks for your valuable time & efforts in advance.
 

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.
You could try this.

VBA Code:
Sub test()
    Dim BrowseWindow As FileDialog
    Dim my_path As String
    Dim dest_Cell As Range
    
    On Error Resume Next

    'dest_cell = something
    
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
    
    If BrowseWindow.Show = -1 Then
        my_path = BrowseWindow.SelectedItems(1) 'Selection of parent folder
        'Proceed
        Call List_Folders_and_Files(my_path, dest_Cell)
    End If
End Sub
 
Upvote 0
You could try this.

VBA Code:
Sub test()
    Dim BrowseWindow As FileDialog
    Dim my_path As String
    Dim dest_Cell As Range
   
    On Error Resume Next

    'dest_cell = something
   
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
   
    If BrowseWindow.Show = -1 Then
        my_path = BrowseWindow.SelectedItems(1) 'Selection of parent folder
        'Proceed
        Call List_Folders_and_Files(my_path, dest_Cell)
    End If
End Sub
In the above code, i made the following substitution:

'dest_cell = something
dest_Cell = ActiveSheet.Range("B2")

The FileDialogFolderPicker box appears and i am able to select the folder, but the operation returns a blank sheet - meaning the data does not get populated. Pls help!
 
Upvote 0
You could try this.

VBA Code:
Sub test()
    Dim BrowseWindow As FileDialog
    Dim my_path As String
    Dim dest_Cell As Range
   
    On Error Resume Next

    'dest_cell = something
   
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
   
    If BrowseWindow.Show = -1 Then
        my_path = BrowseWindow.SelectedItems(1) 'Selection of parent folder
        'Proceed
        Call List_Folders_and_Files(my_path, dest_Cell)
    End If
End Sub
Hello @Formula11
Thank you so much for the efforts once again. The original author of the code @John_w has shared the newer version of the main routine, and that works perfect, like a charm.
Sincere gratitide to you as well as @John_w for sparing your valuabe time and efforts. God bless!

The newer version of the main routine is available here:

This post can be considered closed.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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