How Can I Merge Multiple .CSV Files Into One?

pcguy

New Member
Joined
Feb 14, 2016
Messages
3
I have nearly 50 .csv files that each have one column of data (in column A) and no column headers.

Is there a simple way to merge them all into one big .csv file?

I've tried some online utilities, but I haven't been able to get them to work properly.

I just want to create a REALLY long column A that aggregates the column A's from all of the 50 .csv's into one .csv.

Thanks for your time.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Put all of the CSV files into a folder and change the 'FolderPath' variable to that folder path.

Code:
Option Explicit

Sub CombineCsvs()


Dim FolderPath As String
Dim FileName As String
Dim wbResult As Workbook
Dim WB As Workbook


  FolderPath = "C:\TempFolder"
  If FolderPath Like "*[!\/]" Then
    FolderPath = FolderPath & "/"
  End If
  
  FileName = Dir(FolderPath & "*.csv")
  
  Set wbResult = Workbooks.Add
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Do While FileName <> vbNullString
    Set WB = Workbooks.Open(FolderPath & FileName)
    WB.ActiveSheet.UsedRange.Copy wbResult.ActiveSheet.UsedRange.Rows(wbResult.ActiveSheet.UsedRange.Rows.Count).Offset(1).Resize(1)
    WB.Close False
    FileName = Dir()
  Loop
  
  wbResult.ActiveSheet.Rows(1).EntireRow.Delete
  
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
  
End Sub
 
Upvote 0
Put all of the CSV files into a folder and change the 'FolderPath' variable to that folder path.

Code:
Option Explicit

Sub CombineCsvs()


Dim FolderPath As String
Dim FileName As String
Dim wbResult As Workbook
Dim WB As Workbook


  FolderPath = "C:\TempFolder"
  If FolderPath Like "*[!\/]" Then
    FolderPath = FolderPath & "/"
  End If
  
  FileName = Dir(FolderPath & "*.csv")
  
  Set wbResult = Workbooks.Add
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Do While FileName <> vbNullString
    Set WB = Workbooks.Open(FolderPath & FileName)
    WB.ActiveSheet.UsedRange.Copy wbResult.ActiveSheet.UsedRange.Rows(wbResult.ActiveSheet.UsedRange.Rows.Count).Offset(1).Resize(1)
    WB.Close False
    FileName = Dir()
  Loop
  
  wbResult.ActiveSheet.Rows(1).EntireRow.Delete
  
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
  
End Sub

Is that a .vbs script? Also, where does it dump the file it creates?
 
Upvote 0
Yep, it's an excel macro. You can open excel, hold down the 'Alt' key, press F11, I, M, release 'Alt' and paste the code into the window that appears. F5 to run.
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,841
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top