VBA: List of all files contained within a Directory
The following code is a subroutine that will get the names of all the files that are present in a directory path:
Sub List_All_The_Files_Within_Path()
Dim Row_No As Integer
Dim No_Of_Files As Integer
Dim kk25 As Integer
Dim File_Path As String
File_Path = "C:\My Documents"
Row_No = 36
'Lists all the files in the current directory
With Application.FileSearch
.NewSearch
.LookIn = File_Path
.Filename = "*.*"
.SearchSubFolders = False
.Execute
No_Of_Files = .FoundFiles.Count
For kk25 = 1 To No_Of_Files
Worksheets("Sheet1").Cells(kk25 + 5, 15).Value = .FoundFiles(kk25)
Next kk25
End With
End Sub
It will write the filenames to Sheet1 in column O – starting at row 36.


Keep in mind this will only work pre-Excel 2007. Check out the Scripting.FileSystemObject for a way you can do this in all Excel versions.
-JP
Just to note that Application.FileSearch no longer exists in EXCEL 2007.
Keep in mind that the FileSearch object is no longer available in Office 2007. So this code works only with earlier versions.
You can use the old Dir command or FileSystemObject.