How to List All Files in a Folder Using Excel VBA

VBA & macros · Updated November 2, 2026

How to List All Files in a Folder Using Excel VBA

To list files directly inside a folder in Excel for Microsoft 365 on Windows, run a VBA macro that reads the folder’s file collection and writes each file name to a worksheet. The example below also records each file’s full path, size, and last-modified date; it does not include files in subfolders.

What “all files inside a folder” means

A folder can contain files, subfolders, or both. A basic file listing usually means the files stored immediately in the selected folder, not the files nested in its subfolders. For example, if C:\Reports contains January.xlsx and a subfolder named Archive, a nonrecursive listing returns January.xlsx but does not look inside Archive.

The distinction matters when you are building an inventory, preparing a batch import, or checking which documents are present. “List files” also does not necessarily mean “open the files.” The macro here reads file-system details and writes them into Excel; it does not open, edit, or copy the files themselves.

The code uses the Windows FileSystemObject through late binding, so you do not have to add a reference manually in the VBA editor. It assumes there is a worksheet named File List in the workbook containing the macro. Create that sheet first, or change the worksheet name in the code to match your workbook.

VBA macro to list files in a folder

Change folderPath to the folder you want to inspect. The example path is illustrative; use a real folder path available on your computer. The macro places one file per row, starting in row 2, with the name, full path, size in bytes, and last-modified time in separate columns.

Sub ListFilesInFolder()
    ' Change this to the folder you want to list.
    Dim folderPath As String
    Dim fso As Object
    Dim folder As Object
    Dim fileItem As Object
    Dim ws As Worksheet
    Dim rowNum As Long

    folderPath = "C:\Reports"
    Set ws = ThisWorkbook.Worksheets("File List")

    ' Use late binding so no reference needs to be enabled.
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FolderExists(folderPath) = False Then
        MsgBox "Folder not found: " & folderPath, vbExclamation
        Exit Sub
    End If

    Set folder = fso.GetFolder(folderPath)

    ' Clear only the output columns on the dedicated listing sheet.
    ws.Range("A:D").ClearContents
    ws.Cells(1, 1).Value = "File name"
    ws.Cells(1, 2).Value = "Full path"
    ws.Cells(1, 3).Value = "Size (bytes)"
    ws.Cells(1, 4).Value = "Last modified"

    rowNum = 2
    For Each fileItem In folder.Files
        ws.Cells(rowNum, 1).Value = fileItem.Name
        ws.Cells(rowNum, 2).Value = fileItem.Path
        ws.Cells(rowNum, 3).Value = fileItem.Size
        ws.Cells(rowNum, 4).Value = fileItem.DateLastModified
        rowNum = rowNum + 1
    Next fileItem

    If rowNum = 2 Then
        ws.Cells(2, 1).Value = "No files found."
    End If

    ws.Columns("A:D").AutoFit
End Sub

Before running any macro, save a copy of the workbook. Macro actions cannot be undone with Ctrl+Z, and this one clears the contents of columns A through D on the File List sheet before writing the new results. Keep that sheet dedicated to the listing, because anything already in those four columns will be removed.

How to add and run the macro

  1. In Excel, create a worksheet and name it File List. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) so the VBA code can be retained.
  2. Open the Visual Basic Editor with Alt+F11 on Windows. In the editor, choose Insert > Module, then paste the macro into the module.
  3. Replace C:\Reports with the folder path you want to scan. A path copied from File Explorer can be pasted between the quotation marks, for example C:\Users\Jordan\Documents\Invoices.
  4. Return to Excel and run the macro from the editor with F5, or use Excel’s Developer tab and choose Macros. If the Developer tab is not visible, enable it in Excel Options under Customize Ribbon.

The keyboard shortcut for opening the VBA editor on Mac may be Option+F11; on some keyboards, you may also need the Fn key. Ribbon locations and permissions differ on Mac. This particular example is intended for Excel on Windows because it creates the Windows Scripting FileSystemObject. Excel for the web cannot run VBA macros. A Mac user should use a Mac-compatible approach, such as VBA’s Dir function, and check the path syntax and file-access permissions for that Mac.

Understanding the output and adapting it

Each row represents one file returned from the selected folder. The first column is the file name, such as budget.xlsx; the second is its full path, such as C:\Reports\budget.xlsx. The size is reported in bytes, and the final column contains the file system’s last-modified date and time. Excel may display that date according to the cell’s existing number format or regional settings.

The full path is useful when names repeat. Two different folders can contain a file called summary.pdf, so a name-only list may not identify which copy you mean. You can sort or filter the output using Excel’s normal table and worksheet tools. If you want an Excel table, select the results and use Insert > Table; this makes filtering and sorting the columns convenient.

To produce a simpler inventory, remove the lines that write the size and modified date, along with their headers, and leave the name and path columns. To include an extension column, you could extract the text after the last period from a file name, but remember that file names can contain more than one period: report.final.xlsx has an extension of .xlsx, not .final.xlsx. For precise extension handling, use the file system object’s extension-name property rather than splitting at the first period.

Listing files in subfolders

The macro uses folder.Files, which reads the files in the chosen folder only. It does not descend into child folders. That behavior is often preferable for a quick inventory because it avoids unexpectedly producing a very large list or scanning unrelated archives.

To include nested folders, the code needs to visit each subfolder as well as the starting folder. A typical recursive design has a procedure that writes the files in the current folder, then calls itself for each child folder. Include the full path in the results so that files with identical names in different locations remain distinguishable. For a large directory tree, also consider how many rows Excel can hold and whether the macro should skip protected locations or stop after a chosen folder depth.

Do not assume that a recursive search is harmless just because it only creates a list. It may take much longer on a network share or a directory containing many thousands of items. A recursion routine also needs to handle access-denied errors and avoid following links in ways that could revisit the same locations. If you only need a one-level inventory, the simpler macro above is easier to review and maintain.

Common problems and practical checks

  • “Folder not found” appears: Check spelling, drive letters, and the path in the quotation marks. A mapped network drive may not be available in the context where Excel is running; try the network’s UNC path if your organization permits it.
  • The macro reports a missing worksheet: Create a sheet named exactly File List, including the space, or update the worksheet name in the code. Worksheet names are not interchangeable with workbook file names.
  • No files are listed: Confirm that the path points to the folder itself, rather than to a file or a neighboring folder. The folder may be empty, or its files may be inside subfolders that this macro does not search.
  • Excel blocks the macro: Macro security settings may prevent code from running. Only enable or run macros from workbooks you trust, and follow your organization’s security policy rather than lowering protections broadly.
  • Some folders cause an error: Your Windows account may not have permission to inspect them. Try a folder you own or ask the system administrator about access; do not treat an incomplete listing as proof that no other files exist.
  • Results appear old or duplicated: The output sheet is cleared in columns A:D each run, but the source folder is not changed. Check that the path is the intended one and that the listing was refreshed after files were added, renamed, or removed.

Safety, privacy, and scope

This code reads names and basic metadata; it does not upload files or transmit the list. Still, a file inventory can reveal private information through names, folder paths, and timestamps. Store and share the workbook according to the same rules that apply to the files it describes. If you do not need full paths, omit that output column before sharing the results.

File-system visibility depends on permissions and the location being scanned. A listing should be treated as the files the macro could enumerate at that time, not as a guaranteed audit of every item on a computer or network. If you need a formal compliance inventory, use an approved auditing tool and define whether hidden items, links, protected folders, network locations, and subfolders are in scope.

Frequently Asked Questions

Does this VBA list files in subfolders too?
No. It lists files directly inside the folder named in folderPath. Searching subfolders requires additional traversal code, usually a recursive procedure.

Can I list only PDF or Excel files?
Yes. Add a condition inside the file loop that checks each file’s extension, then write the row only when it matches the types you want. Decide whether extensions should be compared without regard to letter case so that names ending in .PDF are not missed.

Will the macro open or change the listed files?
No. It reads file names, paths, sizes, and modification dates and writes those details to the worksheet. It does not open the listed documents or alter their contents.

Can I run this macro in Excel for the web?
No. Excel for the web can open macro-enabled workbooks, but it does not run VBA macros. Run the macro in the Excel desktop application on a compatible computer.

Why is the macro clearing cells on my listing sheet?
It clears columns A:D so that an older, longer listing does not remain beneath a newer, shorter one. Use a dedicated output sheet, or change the clearing line if those columns contain data you need to keep.

Choosing the right listing method

For a one-time check, File Explorer can display a folder’s contents without writing VBA. A macro is more useful when the list needs to land in a worksheet repeatedly, include metadata, or support a regular review. For very large or organization-wide inventories, a purpose-built file-management or auditing tool may be more suitable than Excel, particularly when access controls and a complete audit trail matter.

Start with a small, accessible folder and confirm that the displayed names and paths match what you expect. Once the scope is clear, decide whether the task needs only direct files, a filtered set of extensions, or a recursive search. That choice determines whether this short macro is sufficient or whether the listing procedure needs additional safeguards.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.