VBA – List of all files contained within a Directory
List All Filenames in Directory
The following code is a subroutine that will get the names of all the files that are present in a directory path:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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
Download the Excel file here
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!Did you find this VBA tutorial useful? Then share it with your friends and colleagues:
Was this helpful? You might like these related Code Examples...