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