Excel Count Files in Folder: 4 Easy Ways
In this article
To count files in a folder directly from Excel, you can use a formula that combines the COUNTA function with INDEX, or you can use a simple VBA macro. The easiest no-code method is to use the FILES function in an Excel defined name, which returns a list of file names that you then count with COUNTA. This article explains several methods, their limitations, and how to choose the right one for your situation.
Why Count Files from Excel?
You might need a quick inventory of files in a folder for reporting, auditing, or data management. Instead of manually copying file names or using a separate file explorer, Excel can pull the list directly. This is especially useful when you need to combine file counts with other data you already have in a spreadsheet, such as project tracking or document logs.
Method 1: Using the FILES Function with a Defined Name
The FILES function is a legacy Excel 4 macro function that still works in modern versions of Excel. It returns an array of file names from a specified folder path. You cannot use it directly in a cell; you must define it as a named formula.
- Press Ctrl+F3 to open the Name Manager.
- Click New.
- In the Name box, type
FileList. - In the Refers to box, enter:
=FILES("C:\YourFolder\*")(replace the path with your actual folder). - Click OK and close the Name Manager.
- In a cell, enter the formula:
=COUNTA(FileList)
This will return the number of files (not subfolders) in the specified folder. The FILES function also supports wildcards like *.xlsx to count only certain file types.
Method 2: Using VBA to Count Files
For more control, a simple VBA macro can count files and even list their names. This is useful when you need to count files recursively in subfolders or want to filter by attributes like date modified.
- Press Alt+F11 to open the VBA editor.
- Go to Insert > Module.
- Paste the following code:
Function CountFiles(folderPath As String) As Long
Dim file As String
Dim count As Long
file = Dir(folderPath & "\*.*")
Do While file <> ""
count = count + 1
file = Dir
Loop
CountFiles = count
End Function
Then in a cell, use =CountFiles("C:\YourFolder"). This function counts all files in the top-level folder. To include subfolders, you would need a more complex recursive function.
Method 3: Using Power Query (Get & Transform)
Power Query is a built-in Excel tool that can import a list of files from a folder. This method is more robust and allows you to refresh the data with one click.
- Go to Data > Get Data > From File > From Folder.
- Browse to your folder and click OK.
- In the preview window, click Transform Data.
- In Power Query, you can filter rows, group by extension, and then load the result to a worksheet.
- To count all files, simply note the number of rows in the preview, or close & load to a table and use
COUNTAon the file name column.
Power Query is ideal when you need to refresh the count regularly or when you want to analyze file metadata like size or date.
Method 4: Using a File Dialog (Manual)
If you only need a one-time count, you can use the file dialog in Excel to select multiple files and see the count in the title bar.
- Press Ctrl+O to open the Open dialog.
- Navigate to the folder.
- Select all files (Ctrl+A) – the dialog shows the number of items selected at the bottom.
This is not a formula, but it works without any setup. It does not give you a cell value, so it is only for quick reference.
Common Problems and Limitations
- FILES function returns #VALUE! error – This happens if the path is incorrect or if the folder does not exist. Double-check the path and ensure it ends with a backslash and wildcard.
- FILES function does not update automatically – It is a volatile function, but it may not refresh when files are added or removed. Press F9 to recalculate.
- Hidden files are counted – The
FILESfunction and VBADirinclude hidden files by default. To exclude them, you need to filter with attributes. - Subfolders are not included – The basic methods only count files in the top folder. To include subfolders, use a recursive VBA macro or Power Query with “Include subfolders” option.
- Path length limits – Windows has a 260-character path limit. Very deep folder structures may cause errors.
How to Choose the Right Method
Consider the following factors when deciding which method to use:
- Frequency – If you need a one-time count, use the file dialog or a simple formula. For recurring counts, use Power Query or a saved macro.
- Need for subfolders – Use a recursive VBA macro or Power Query with subfolder inclusion.
- Need for file details – Power Query gives you name, size, date, and more. VBA can also extract these with extra code.
- Excel version – The
FILESfunction works in all modern versions, but Power Query is only available in Excel 2016 and later. - Sharing – If you share the workbook with others, a formula using a defined name may be simpler than a macro, which requires enabling macros.
Frequently Asked Questions
Can I count files in a folder without opening it?
Yes, the FILES function and VBA method both work without opening the folder in File Explorer. You just need the full path.
Why does my FILES function return 0?
Check that the folder path is correct and that you used the wildcard * at the end. Also, ensure the cell containing =COUNTA(FileList) is formatted as a number, not text.
Can I count only certain file types, like .pdf?
Yes, use a wildcard in the path: =FILES("C:\Folder\*.pdf") or in VBA use Dir(folderPath & "\*.pdf").
Does the FILES function work in Excel Online or Google Sheets?
No, the FILES function is specific to desktop Excel. In Google Sheets, you can use a Google Apps Script to count files, but not with a simple formula.
How do I count files in all subfolders recursively?
Use a recursive VBA macro that loops through each subfolder, or use Power Query and enable the "Include subfolders" option in the folder import.
Can I count files based on date modified?
Yes, with VBA you can use the FileDateTime function to filter, or in Power Query you can filter the "Date modified" column after importing.