How to loop through files in a Folder

April 21st, 2007

Here’s a simple routine to loop through all the files in a particular folder. It places all file names in a collection object. You can then do whatever you want with the collection. This particular example lists the file names in column A of the active sheet.

Sub LoopThroughFiles()
Dim strFile As String
Dim strPath As String
Dim colFiles As New Collection
Dim i As Integer

strPath = “D:\TEMP\”
strFile = Dir(strPath)

While strFile <> “”
colFiles.Add strFile
strFile = Dir

‘List filenames in Column A of the active sheet
If colFiles.Count > 0 Then
For i = 1 To colFiles.Count
ActiveSheet.Cells(i, 1).Value = colFiles(i)
Next i
End If

End Sub

Posted in Excel, How To, VBA | No Comments

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

(C) by Virgilio Adriano. All rights reserved. Powered by WordPress.
Entries and comments feeds.
It took 0.207 seconds to load this page.