Expand To Show Full Article
How to List All Files in a Directory in Excel VBA? - DeveloperPublish

How to List All Files in a Directory in Excel VBA?

In this post, you’ll learn how to list all files in a Directory in your excel worksheet programmatically using Excel VBA.

How to List all files in a Directory in Excel VBA?   

Using the FileSystemObject  you can list all files in a folder or directory.

Using the FileSystemObject to Get the List of Files in a Folder

To get the List of Files in a Folder, try using the below VBA code snippet.

For instance, Create a folder in the C drive with the name Excel VBA.

Code:

Sub LoopThroughFiles ()

Dim FSO As Object

Dim Folder As Object

Dim File As Object

Dim i As Integer

Set FSO = CreateObject("Scripting.FileSystemObject")

Set Folder = FSO.GetFolder("C:\Excel VBA ") (# Mention the exact address of the directory here.)

For Each File In Folder.Files

    Cells(i + 1, 1) = File.Name

    i = i + 1

Next File

End Sub
How to List All Files in a Directory in Excel VBA?