VBA macros · Workbooks and files

VBA macro to list all files in a folder in Excel

Write the name, size, date and a clickable link for every file in a folder to a new workbook. Includes a version that also lists subfolders.

Windows + Mac

A list of files is handy for checking which reports have arrived, renaming files in bulk, or documenting a project folder. Windows Explorer cannot export its list to Excel directly. This macro asks for a folder and writes the name, size, last-modified date and a hyperlink for each file into a new workbook.

VBA
Option Explicit

Sub ListFilesInFolder()
    Dim folder As String, f As String
    Dim ws As Worksheet
    Dim r As Long

    folder = PickFolder()
    If Len(folder) = 0 Then Exit Sub
    If Right$(folder, 1) <> Application.PathSeparator Then folder = folder & Application.PathSeparator

    Set ws = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    ws.Range("A1:D1").Value = Array("File name", "Size (KB)", "Last modified", "Link")
    ws.Range("A1:D1").Font.Bold = True
    ws.Columns("A").NumberFormat = "@"
    ws.Columns("C").NumberFormat = "yyyy-mm-dd hh:mm"

    Application.ScreenUpdating = False
    r = 2
    f = Dir(folder)                     ' first file; Dir() gives the next one
    Do While Len(f) > 0
        ws.Cells(r, 1).Value = f
        ws.Cells(r, 2).Value = Round(FileLen(folder & f) / 1024, 1)
        ws.Cells(r, 3).Value = FileDateTime(folder & f)
        ws.Hyperlinks.Add Anchor:=ws.Cells(r, 4), Address:=folder & f, TextToDisplay:="Open"
        r = r + 1
        f = Dir()
    Loop

    ws.Columns("A:D").AutoFit
    Application.ScreenUpdating = True
    MsgBox r - 2 & " file(s) listed.", vbInformation
End Sub

Private Function PickFolder() As String
    ' Windows: folder dialog. Mac: type or paste the path.
    #If Mac Then
        PickFolder = InputBox("Full path of the folder:", "Pick a folder", ThisWorkbook.Path)
    #Else
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Pick a folder"
            If .Show = -1 Then PickFolder = .SelectedItems(1)
        End With
    #End If
End Function
List the files in a folder with size, date and link

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick ListFilesInFolder and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

PickFolder returns the chosen folder path, or an empty string when you cancel. The path separator is added if needed so file names can be appended directly.

Dir(folder) returns the first file in the folder (folders themselves and hidden files are not included). Each later call of Dir() with no arguments returns the next name, and an empty string when there are no more. That makes the Do While Len(f) > 0 loop the classic way to walk through a folder; see Do loops: Do While, Do Until and Exit Do and Files and folders: open, save, loop through files and write text files.

FileLen gives the size in bytes, divided by 1024 for kilobytes, and FileDateTime gives the date and time the file was last changed. Column A is formatted as text first so names like 2026.pdf are not touched, and column C gets a date-time format. Each row ends with an Open hyperlink to the file.

Change it

To list only some file types, wrap the body of the loop in a test, for example If LCase$(f) Like "*.pdf" Then. To include files in all subfolders too, Windows offers the FileSystemObject, which can walk folders recursively:

VBA
Sub ListFilesWithSubfolders()
    Dim fso As Object
    Dim r As Long
    Set fso = CreateObject("Scripting.FileSystemObject")    ' Windows only
    ActiveSheet.Range("A1:C1").Value = Array("Folder", "File name", "Size (KB)")
    r = 2
    ListFolder fso.GetFolder("C:\Reports"), ActiveSheet, r
End Sub

Private Sub ListFolder(fld As Object, ws As Worksheet, ByRef r As Long)
    Dim fil As Object, subFld As Object
    For Each fil In fld.Files
        ws.Cells(r, 1).Value = fld.Path
        ws.Cells(r, 2).Value = fil.Name
        ws.Cells(r, 3).Value = Round(fil.Size / 1024, 1)
        r = r + 1
    Next fil
    For Each subFld In fld.SubFolders
        ListFolder subFld, ws, r                             ' the procedure calls itself
    Next subFld
End Sub
Windows only: list files in a folder and all its subfolders

ListFolder is recursive: after listing a folder's files it calls itself for every subfolder. r is passed ByRef, so every level writes to the next free row. Change C:\Reports to your folder. This version runs only on Windows because the FileSystemObject is not part of Excel for Mac.

What can go wrong

FileLen cannot report files of 2 GB or more correctly; it returns a wrong or negative number. Use the FileSystemObject's Size on Windows for very large files. Folders with tens of thousands of files take a while; screen updating is off to keep it as fast as possible.

Watch out. Clicking an Open link starts the file with its default program. Excel asks for confirmation for some file types; do not open files you do not trust.

On a Mac, FileDialog does not exist, so the #If Mac Then branch asks for the folder path in an input box instead (for example /Users/anna/Documents/Reports). Excel for Mac is sandboxed: the first time a macro opens files in a folder, macOS may ask you to grant access to it. Click Grant Access.

Tip. To rename files listed this way, add the new names in column E and use VBA's Name oldPath As newPath statement in a loop. Test on a copy of the folder first.

Related: VBA macro to merge all workbooks in a folder into one sheet, VBA macro to create a folder for each name in a list, Files and folders: open, save, loop through files and write text files.

More vba macros