Blog/Excel VBA/Loop Through Files in a Folder

VBA Loop Through Files in a Folder: Batch-Import CSVs

vba loop through files in folder: Dir() function batch-importing bank statement CSVs into one workbook

The vba loop through files in folder pattern uses the built-in Dir() function to visit every matching file in a folder, one at a time, without any external library reference — ideal for batch-importing a folder full of bank statement CSVs.

Prashant Panchal
Prashant Panchal

ACA | FMVA® | 19 Years in Finance

This guide is part of the FinDataPro Excel VBA Workbook Events series.

The Dir() Loop

Run this against a folder of bank statement CSVs:

Option Explicit

Sub ImportAllStatements()
    Dim folderPath As String
    Dim fileName As String

    folderPath = "C:\Shared\BankStatements\"
    fileName = Dir(folderPath & "*.csv")

    Do While fileName <> ""
        Workbooks.Open folderPath & fileName
        ' ... copy the relevant range into a consolidated sheet here ...
        ActiveWorkbook.Close SaveChanges:=False
        fileName = Dir()
    Loop
End Sub

Dir() called with no arguments returns the next matching file each time, until it returns an empty string.

Download the Example Workbook — Free

The working .xlsm file with this Dir() import loop already wired up — open it, inspect the code, and point it at your own statements folder.

Enter your email to get instant access — no spam, ever.

Where the Code Goes

This lives in a standard module and pairs directly with Find Last Row — once each statement is open, you need a reliable last-row method to know where to append its data in the consolidated sheet.

Finance Use Case: A Folder Full of Bank Statements

Multiple bank accounts, each exporting a monthly CSV into the same shared folder, is a common reconciliation setup. Opening and copying each file by hand does not scale past two or three accounts. The Dir() loop above opens every CSV in the folder in turn, appends its data, and closes it without saving — one macro run instead of a dozen manual opens.

The manual version of this task also tends to go stale quietly — a new bank account gets added to the mix, its statement lands in the same folder, and whoever is doing the reconciliation by hand simply forgets it exists until the totals do not tie out. Because the loop reads whatever matches the wildcard pattern in the folder at the moment it runs, a new file dropped into BankStatements\ is picked up automatically on the very next run, with nothing to update in the macro itself.

Filtering to Specific File Types

The wildcard passed to the first Dir() call controls what gets matched. *.csv matches only CSVs; Statement_*.xlsx would match only Excel files starting with that prefix. Every subsequent Dir() call with no argument continues that same filtered sequence.

Skip the manual multi-bank import entirely

FinDataPro AutoBankRec imports and matches statements from multiple banks automatically, so there is no folder of CSVs left for this loop to process by hand.

See AutoBankRec

Common Mistakes

  • Calling Dir() a second time inside the loop body for an unrelated check, which resets the internal file pointer and truncates the loop after one file.
  • Forgetting the trailing backslash on folderPath, which silently searches the wrong location.
  • Assuming Dir() searches subfolders — it only ever sees the single folder level given.
  • Not closing each opened workbook before the next Dir() call, which leaves a growing pile of open files by the end of the loop.

Before appending each file's data, confirm your last-row method is reliable, and see GetOpenFilename for the single-file version of this same import problem.

Full reference: Microsoft Learn Dir function reference.

Frequently Asked Questions

Do I need FileSystemObject to loop through files?

No — the built-in Dir() function needs no library reference. FileSystemObject is only worth it for recursive subfolder searching or richer metadata.

How do I loop through only CSV files?

Pass a wildcard pattern like Dir(folderPath & "*.csv") to the first call.

Why does my loop stop after the first file?

A second Dir() call inside the loop resets the internal pointer. Keep exactly one Dir() sequence per loop.

Can this loop through subfolders too?

No — Dir() only sees one folder level. Use FileSystemObject or a recursive Sub for genuine subfolder searching.

Conclusion

A single Dir() loop replaces a manual, per-file import routine with one pass across an entire folder — no library reference required. Anchor it with a reliable last-row method for the append step. If the reconciliation itself is the bottleneck, see AutoBankRec.

Prashant Panchal
Prashant Panchal• ACA | FMVA® | 19 Years in Finance

Prashant Panchal is a Chartered Accountant (ACA) and Financial Modelling & Valuation Analyst (FMVA®) with 19 years of experience in finance, FP&A, and financial modelling across the GCC region. He is the founder of FinDataPro.

Discussion

Leave a Comment

0/2000

Comments are moderated and appear once approved.