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 SubTips
- All files should have the same columns in the same order.
Local:=Truemakes 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
- Open your workbook and press Alt + F11 to open the VBA editor.
- Choose File, Import File and pick the downloaded .bas file.
- Save the workbook as Excel Macro-Enabled Workbook (.xlsm).
- 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
- Send personalised emails from Excel with Outlook
- Lock and unlock every sheet with one button
- Split one sheet into many sheets by column value
- 12 Excel VBA macros that save hours every week
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.
