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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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,215,359
Messages
6,124,488
Members
449,165
Latest member
ChipDude83

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