How to List File Names from Multiple Folders in Excel Cells Using VBA
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.

- 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.
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.
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.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the top menu, click 'Insert' and select 'Module' to create a new blank code window.
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
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.
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.

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. Download and Install: Download and install WPS Office, ensuring you select the version that includes VBA support.
- 2. Enable Developer Tools: Open your spreadsheet in WPS Spreadsheet and navigate to the Developer tab.
- 3. Open VBA Editor: Click on 'VBA Editor' to open the familiar coding environment.
- 4. Run Your Macro: Paste your folder-listing macro and hit Run to automate your file management workflow instantly.

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).




