VBA & Macro Problems

How to List File Names from Multiple Folders in Excel Cells Using VBA

Muhammad TalhaMuhammad Talha Nov 10, 2026 868 views

Question details

The user wants to retrieve file names from multiple distinct directories and output these lists into specific Excel cells using a VBA macro.

How to List File Names from Multiple Folders in Excel Cells Using VBA
Product
Excel
Device & OS
not provided
Scenario
Organizing or auditing files by extracting file names from different folders and documenting them in specific spreadsheet cells.
Observed behavior
A reusable VBA script leveraging the Dir function is required to append matched file names from different paths into designated target cells.
Before you start

Ensure that you have enabled the Developer tab in your spreadsheet and remember to save your workbook as a Macro-Enabled Workbook (.xlsm) to preserve your VBA code.

Solution 1Recommended

Use a Reusable VBA Procedure with the Dir Function

Create a custom VBA macro that uses the Dir function to loop through a specified folder path and append the file names into a designated Excel cell.

VBA can use the built-in Dir function to retrieve file names from a local folder. By creating a reusable subroutine that accepts the folder path and the target cell as arguments, you can easily call it multiple times to list files from entirely different folders into different cells within the same script.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a new blank code window.

3
Paste the VBA Code

Copy and paste the following code into the module window: Sub ListFilesInFolder(folderPath As String, targetCell As Range) Dim fileName As String fileName = Dir(folderPath & "*.xls*") Do While fileName <> "" targetCell.Value = targetCell.Value & fileName & vbCrLf fileName = Dir Loop End Sub Sub ExampleUsage() ListFilesInFolder "C:\FirstFolderPath\", ThisWorkbook.Sheets("Sheet1").Range("A1") ListFilesInFolder "C:\SecondFolderPath\", ThisWorkbook.Sheets("Sheet1").Range("B1") End Sub

4
Customize Folder Paths and Cell References

Modify the folder paths (e.g., "C:\FirstFolderPath\") and the target cell references (e.g., Range("A1")) inside the ExampleUsage subroutine to match your specific directories and worksheet setup.

5
Run the Macro

Click anywhere inside the ExampleUsage subroutine and press F5, or click the 'Run' button in the toolbar. The files will now be listed in your chosen cells.

Use a Reusable VBA Procedure with the Dir Function
Enable Wrap Text: The macro uses vbCrLf to add a line break after each file name. Ensure your target cells have 'Wrap Text' enabled from the Home tab to view the file list properly on multiple lines.
Efficient Spreadsheet Management

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Office provides robust support for VBA macros, allowing you to automate repetitive tasks like listing files from multiple folders just as you would in Microsoft Excel. It is a lightweight, highly compatible, and feature-rich spreadsheet alternative.

  1. 1. Download and Install: Download and install WPS Office, ensuring you select the version that includes VBA support.
  2. 2. Enable Developer Tools: Open your spreadsheet in WPS Spreadsheet and navigate to the Developer tab.
  3. 3. Open VBA Editor: Click on 'VBA Editor' to open the familiar coding environment.
  4. 4. Run Your Macro: Paste your folder-listing macro and hit Run to automate your file management workflow instantly.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled formats.Built-in Developer tools for writing, editing, and debugging VBA code effortlessly.Lightweight installation footprint with incredibly fast loading times.Cost-effective and comprehensive office suite for all your document needs.
microsoft office alternative - wps office

Frequently Asked Questions

Can I filter for specific file types other than Excel files?

Yes, you can modify the Dir function parameter in the code. For example, change `Dir(folderPath & "*.xls*")` to `Dir(folderPath & "*.pdf")` to list only PDF files, or use `Dir(folderPath & "*.*")` to list all files in the folder regardless of their extension.

Why are the file names appearing on a single continuous line in my cell?

The macro inserts a line break (`vbCrLf`) between file names, but Excel cells do not display line breaks by default. You must select the target cells, go to the Home tab, and click 'Wrap Text' for the file names to display correctly on separate lines.

Does this VBA code list files hidden in subfolders?

No, the standard `Dir` function used in this script only retrieves files from the top-level directory specified in the folder path. To include files from subfolders, you would need to implement a recursive VBA procedure or use the FileSystemObject (FSO).