Hello mc84excel ,
Try this macro I wrote using the Dir function.
Recursively List Files and Folders Using Dir
Private SubFolders As Collection
Sub ListFiles(ByVal Folder_Path As String, ByRef Rng As Range, Optional ByVal Include_Subfolders As Boolean)
Dim FileName As String
Dim FilePath As String
Dim Filespec As String
Dim row As Long
Dim SubFolder As Variant
If SubFolders Is Nothing Then Set SubFolders = New Collection
FilePath = IIf(Right(Folder_Path, 1) <> "\", Folder_Path & "\", Folder_Path)
FileName = Dir(FilePath & "*.*", vbDirectory)
Do While FileName <> ""
Filespec = FilePath & FileName
If (GetAttr(Filespec) And vbDirectory) = vbDirectory Then
If FileName <> "." And FileName <> ".." And Include_Subfolders Then SubFolders.Add Filespec
If row = 0 Then
Rng.Offset(row, 0).Font.Bold = True
Rng.Offset(row, 0) = FilePath
End If
Else
row = row + 1
Rng.Offset(row, 1) = FileName
End If
FileName = Dir()
Loop
If Include_Subfolders And SubFolders.Count <> 0 Then
SubFolder = SubFolders.Item(1)
SubFolders.Remove 1
Call ListFiles(SubFolder, Rng.Offset(row + 1, 0), True)
End If
End Sub
Example of Calling the Macro
This will list all files and subfolders on the ActiveSheet in columns "A:B". Folder names are in bold in "A" and file names in "B".
Sub ListFilesTest()
Dim MyPath As String
MyPath = "C:\Test" ' <<<<< Change Folder to one you want to use.
ListFiles MyPath, Range("A1"), True
End Sub
Bookmarks