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?

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn how to Find Blank cells in excel so that you can remove or delete the blank cells form...
In this article, you’ll learn what is a Gauge Chart in Microsoft Excel. Also, you will learn how to add...
Microsoft Excel provides a shortcut for the users to move columns in excel using two different ways – using Shift...