Combine all CSV files in a folder into one sheet with VBA

Pick a folder and this macro stacks every CSV file into one clean sheet, keeps a single header row and records which file each row came from.

Cover for the guide: Combine all CSV files in a folder into one sheet with VBA

Many systems export one CSV file per day, per branch or per user. Opening each file and copying the rows into a master sheet is slow and easy to get wrong. This macro does it in one step.

What it does

  • Asks you to choose a folder.
  • Opens every .csv file in it, one at a time.
  • Copies the header row from the first file only, then the data rows from every file.
  • Adds a Source file column so you can trace any row back to its file.
  • Puts everything on a new sheet and auto-fits the columns.

The macro

Option Explicit

' Combine every CSV file in a folder into one new sheet
' Free module from sourabsaha.com
'
' The header row is taken from the first file. Every file after that
' adds its data rows only, and a "Source file" column shows where each row came from.

Public Sub CombineCsvFiles()
    Dim folder As String, fileName As String
    Dim target As Worksheet, src As Workbook
    Dim nextRow As Long, lastRow As Long, lastCol As Long
    Dim sourceCol As Long, fileCount As Long

    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Choose the folder that holds your CSV files"
        If .Show <> -1 Then Exit Sub
        folder = .SelectedItems(1) & Application.PathSeparator
    End With

    fileName = Dir(folder & "*.csv")
    If fileName = "" Then
        MsgBox "No CSV files were found in that folder.", vbExclamation
        Exit Sub
    End If

    Application.ScreenUpdating = False
    Set target = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    nextRow = 1

    Do While fileName <> ""
        Set src = Workbooks.Open(folder & fileName, Local:=True)
        With src.Worksheets(1)
            lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

            If fileCount = 0 Then
                ' First file: copy the header row as well
                .Range(.Cells(1, 1), .Cells(1, lastCol)).Copy target.Cells(1, 1)
                sourceCol = lastCol + 1
                target.Cells(1, sourceCol).Value = "Source file"
                nextRow = 2
            End If

            If lastRow > 1 Then
                .Range(.Cells(2, 1), .Cells(lastRow, lastCol)).Copy target.Cells(nextRow, 1)
                target.Range(target.Cells(nextRow, sourceCol), target.Cells(nextRow + lastRow - 2, sourceCol)).Value = fileName
                nextRow = nextRow + lastRow - 1
            End If
        End With
        src.Close SaveChanges:=False
        fileCount = fileCount + 1
        fileName = Dir()
    Loop

    target.Rows(1).Font.Bold = True
    target.Columns.AutoFit
    Application.ScreenUpdating = True
    MsgBox fileCount & " file(s) combined into " & nextRow - 2 & " rows.", vbInformation
End Sub

Tips

  • All files should have the same columns in the same order.
  • Local:=True makes Excel read dates and decimals with your regional settings. Remove it if your files use US formats and your PC does not.
  • For very large files, Power Query (Data, Get Data, From Folder) is another good option. I often combine both: Power Query for the import and VBA for the reporting.

Free download

Download Combine-CSV-Files.bas

  1. Open your workbook and press Alt + F11 to open the VBA editor.
  2. Choose File, Import File and pick the downloaded .bas file.
  3. Save the workbook as Excel Macro-Enabled Workbook (.xlsm).
  4. Run the macro from Developer, Macros, or assign it to a button.

Common problems and quick fixes

  • Numbers arrive as text. Some exports put spaces or quotes around numbers. Add a Value = Value pass on the number columns after the import.
  • Dates turn into US or UK format by mistake. Open the CSV with Local:=True so Excel reads dates the way your Windows region expects.
  • The header row repeats for every file. The macro copies the header only from the first file and skips row one in the rest. Keep that check if you change the code.
  • Special characters look wrong. Save the source files as UTF-8, or read them with Power Query, which handles text encoding better than Workbooks.Open.

Frequently asked questions

How many files can it handle?

Hundreds. The limit is the Excel row count of about one million rows. For more than that, load the files into Power Query or a database instead.

Can it add the file name to every row?

Yes. Write the file name into an extra column for the rows you just pasted, so you always know where each line came from.

Will it pick up new files automatically?

Run it again and it reads whatever is in the folder at that moment. You can also run it on open, or every morning with Windows Task Scheduler.

Related guides

Need this built for your own workbook? I write import, clean-up and merge macros for US businesses every week, tested on a copy of your file and delivered with a short how-to video. See my Excel VBA automation service or get a free quote in under a minute.

12 Excel VBA macros that save hours every week

The small macros I use on almost every client file. One .bas file you import once, with clear comments and step by step instructions.

  • Unhide, protect and unprotect all sheets
  • Delete blank rows and clean messy text
  • Highlight duplicates in any range
  • Split one sheet into many by a column
  • Combine every sheet into one
  • Export each sheet to its own PDF
  • Clickable sheet index, dated backups and a formula audit list

No spam. Your email is never shared. Privacy Policy

Sourab Saha, Data Analyst

About the author

Sourab Saha · Data Analyst · Kolkata, India

I’m Sourab Saha, a data analyst and automation developer from Kolkata, India, with more than ten years of experience turning manual data work into clean, automated systems. On Fiverr and Upwork I have delivered over 500 data and automation projects for clients in the US, UK, Europe, Australia and the Middle East, with a 5.0 rating on both platforms. My toolkit is Excel VBA, SQL, Power BI and DAX, Python with Selenium, and Google Apps Script. I collect data from files, databases and websites, clean it, model it and present it so decisions take minutes instead of days. I build and test every tool on your real data, deliver it with a short walkthrough, and keep revising until it works exactly the way you need. Alongside my freelance work, since 2024 I have also worked as a Data Analyst for AH Group of Industries, a US healthcare group, building automated reports and Power BI dashboards from billing, payroll, audit and attendance data.

Rated 5.0 on Fiverr and Upwork, with more than 500 automation projects delivered and over ten years of Excel VBA. You get a fixed price before any work starts, your files stay private on your order page, and every order comes with a proper invoice.

WhatsApp