VBA List Files in Folder

Associated Files Download Links

VBA allows you to list all files from a folder, using the FileSystemObject. In this tutorial, you will learn how to get names of all files in a folder and put them into a Worksheet.

If you want to learn how to check if a file exists, you can click on this link: VBA File Exists 

 

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

We will show how to get a list of files in the folder C:\VBA Folder and put it into the first column of the Worksheet. This folder consists of 5 files, as shown in Image 1:

Image 1. Files in folder C:\VBA Folder

 

Here is the code:

 

In the example, you first need to create the object of the class Scripting.FileSystemObject:

 

Then you have to set the folder using the method GetFolder of the class to the oFolder:

 

Now you can loop through oFolder, in the attribute oFile.Name get the name of every file in the folder and write it in the next empty row:

 

Image 2. Worksheet with the list of files in the folder

 

As you can see in Image 2, all 5 files from the C:\VBA Folder are listed in the first column.

You may also like some of this related content...

VBA allows you to open or close files using the standard methods .Open and .Close.
VBA allows you to check if a file or folder exists by using the Dir
VBA allows you to rename an existing file, using the Name command. In this tutorial,
VBA allows you to delete an existing file, using the Kill command. In this tutorial,
VBA allows you to choose a file to open using the Application.GetOpenFilename method. In this
Ads