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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about
Code:
[COLOR=#ff0000]Dim FileList() As Variant[/COLOR]


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.ListBox1.List = FileList
End Sub
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
      ReDim Preserve FileList(i)
         FileList(i) = FldrFile
         i = i + 1
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub
The line in red must go at the very top of the module, before any code
 
Upvote 0
How about
Code:
[COLOR=#ff0000]Dim FileList() As Variant[/COLOR]


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.ListBox1.List = FileList
End Sub
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
      ReDim Preserve FileList(i)
         FileList(i) = FldrFile
         i = i + 1
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub
The line in red must go at the very top of the module, before any code

this line in red brings Compile error: Member already exists in an object module from which this object module derives
 
Upvote 0
In that case try changing the name, like
Code:
Dim MyFileList() As Variant


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.ListBox1.List = MyFileList
End Sub
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
      ReDim Preserve MyFileList(i)
         MyFileList(i) = FldrFile
         i = i + 1
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub
 
Upvote 0
Have you tried copying & pasting the code I supplied into your file?
 
Upvote 0
In that case try changing the name, like
Code:
Dim MyFileList() As Variant


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.ListBox1.List = MyFileList
End Sub
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
      ReDim Preserve MyFileList(i)
         MyFileList(i) = FldrFile
         i = i + 1
   Next FldrFile
   
   If IncludeSubFolders Then
      For Each SubFldr In Fldr.subfolders
         Call RecursiveFolder(FSO, SubFldr, True)
      Next SubFldr
   End If
End Sub
I got it... it works
 
Upvote 0
How do you want to shorten them?
Just show the filename without the path?
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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