Listing Sub Folders

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
I have this code this list the files of a folder into a ListBox called "Filelist". how can i modify the code to include the files in its subfolders?
Code:
Function FileArray(Path As String)    Dim Name As String, Counter As Integer, Files() As String
    Name = Dir("C:\Users\USER\Documents\*.xls", vbNormal)
    Counter = 0
    Do While Name > ""
        If Name > "." And Name > ".." Then
            ReDim Preserve Files(Counter)
            Files(Counter) = Name
            Counter = Counter + 1
        End If
        Name = Dir
    Loop
    FileArray = Files()
End Function




Private Sub UserForm_Initialize()
    Dim Files As Variant, NewDocument As Variant, Folder As String
    Folder = "C:\Users\USER\Documents\"  ' ENTER PATH HERE
    Files = FileArray(Folder)
    For Each NewDocument In Files
       FileList.AddItem NewDocument 'ASSUMING U HAVE A LISTBOX FileList IN THE FORM
    Next
End Sub
 
In the properties window for FileList listbox change the ColumnCount to -1 and then use
Code:
Private Sub UserForm_Initialize()
   Dim FSO As Object
   Dim StartFldr As Object
   Dim StartPth As String
   
   StartPth = "C:\Mrexcel"
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Call RecursiveFolder(FSO, StartFldr, True)
   Me.FileList.ColumnWidths = "0;200"
End Sub
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   
   For Each FldrFile In Fldr.Files
      With Me.FileList
         .AddItem FldrFile
         .List(.ListCount - 1, 1) = FldrFile.Name
      End With
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub

Private Sub OK_Click()
    Workbooks.Open FileList.Value
End Sub
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
In the properties window for FileList listbox change the ColumnCount to -1 and then use
Code:
Private Sub UserForm_Initialize()
   Dim FSO As Object
   Dim StartFldr As Object
   Dim StartPth As String
   
   StartPth = "C:\Mrexcel"
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Call RecursiveFolder(FSO, StartFldr, True)
   Me.FileList.ColumnWidths = "0;200"
End Sub
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   
   For Each FldrFile In Fldr.Files
      With Me.FileList
         .AddItem FldrFile
         .List(.ListCount - 1, 1) = FldrFile.Name
      End With
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub

Private Sub OK_Click()
    Workbooks.Open FileList.Value
End Sub

Great!
But I just realised that I displays all files. Where can i modify to make it list only "*.xlsx" files?
 
Upvote 0
Add these two lines as shown
Code:
   For Each FldrFile In Fldr.Files
     [COLOR=#ff0000] If FSO.GetExtensionName(FldrFile) Like "xlsx" Then[/COLOR]
         With Me.ListBox1
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
     [COLOR=#ff0000] End If[/COLOR]
   Next FldrFile
If you want any xl files change the xlsx to xl*
 
Upvote 0
Add these two lines as shown
Code:
   For Each FldrFile In Fldr.Files
     [COLOR=#ff0000] If FSO.GetExtensionName(FldrFile) Like "xlsx" Then[/COLOR]
         With Me.ListBox1
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
     [COLOR=#ff0000] End If[/COLOR]
   Next FldrFile
If you want any xl files change the xlsx to xl*

i added it to
Code:
[COLOR=#333333][I]Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)[/I][/COLOR]
, it did not to work
 
Upvote 0
another issue is that this code
Code:
   Dim FSO As Object   Dim StartFldr As Object
   Dim StartPth As String
   
   StartPth = folder_textbox.text & "\"
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set StartFldr = FSO.getFolder(StartPth)
   Call RecursiveFolder(FSO, StartFldr, True)
   Me.FileList.ColumnWidths = "0;200"

I want it to update each time I change the path through a textbox called "folder_textbox"
 
Upvote 0
Can you please post the code you have for that sub?
 
Upvote 0
this is how i modified it
Code:
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   
   For Each FldrFile In Fldr.Files
      With Me.FileList
         .AddItem FldrFile
         .List(.ListCount - 1, 1) = FldrFile.Name
      End With
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
    For Each FldrFile In Fldr.Files
      If FSO.GetExtensionName(FldrFile) Like "xls" Then
         With Me.ListBox1
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
      End If
   Next FldrFile
End Sub
 
Last edited:
Upvote 0
You should have added the two lines in red not the entire code, like
Code:
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   Static i As Long
   
   For Each FldrFile In Fldr.Files
      If FSO.GetExtensionName(FldrFile) Like "xls*" Then
         With Me.ListBox1
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
      End If
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub
 
Upvote 0
You should have added the two lines in red not the entire code, like
Code:
Sub RecursiveFolder(FSO As Object, Fldr As Object, IncludeSubFolders As Boolean)
   Dim FldrFile As Object, SubFldr As Object
   Static i As Long
   
   For Each FldrFile In Fldr.Files
      If FSO.GetExtensionName(FldrFile) Like "xls*" Then
         With Me.ListBox1
            .AddItem FldrFile
            .List(.ListCount - 1, 1) = FldrFile.Name
         End With
      End If
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub

oh! i see. thanks
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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