Share via

VBA code to merge and import Multiple CSVs

Anonymous
2021-01-20T04:45:55+00:00

So I have this VBA  code which will merge and import multiple CSV files into one. Can someone help me with these two issues I am facing?

  1. Now I am trying to copy these merged CSVs into the existing workbook, say WB1 (from where I am running this code) . However, it is rather creating a new workbook, say WB2, with merged data. Is it possible to copy them all to a sheet in WB1 itself?
  2. Also, all the individual CSV files contain headers, which I don't need each time a new file copies. Is there anyway to avoid the headers from getting copied from each individual csv?

Option Explicit

Sub CombineCsvs()

Dim FolderPath As String

Dim FileName As String

Dim wbResult As Workbook

Dim WB As Workbook

  FolderPath = "C:\testfolder\inputfiles"

  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

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2021-01-21T12:07:39+00:00

    You have wbResult created in the wrong place

    The following will import all the CSVs from 'FolderPath' into the active sheet of the new workbook 'wbResult' , omitting the header rows

    Option Explicit

    Sub CombineCsvs()

    Dim FolderPath As String

    Dim FileName As String

    Dim wbResult As Workbook

    Dim WB As Workbook

      FolderPath = "C:\testfolder\inputfiles"

      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.Rows(1).EntireRow.Delete

        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

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-01-20T05:19:43+00:00

    Hey @Andreas Killer, thanks for sharing that.

    I haven't really worked on power query before but this is something I really wanted to learn. Thanks for sharing this, I will take a look at it and try to replicate it. :)

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2021-01-20T05:12:54+00:00

    Move the workbook creation out of the loop i.e.

    Set wbResult = Workbooks.Add

        FileName = Dir(FolderPath & "*.csv")

    Thanks a lot for the quick response.

    I tried running it but it is not copying the data but rather just creating a blank new workbook:

    Option Explicit

    Sub CombineCsvs()

    Dim FolderPath As String

    Dim FileName As String

    Dim wbResult As Workbook

    Dim WB As Workbook

      FolderPath = "C:\testfolder\inputfiles"

      If FolderPath Like "*[!/]" Then

        FolderPath = FolderPath & "/"

      End If

      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

     FileName = Dir(FolderPath & "*.csv")

      Set wbResult = Workbooks.Add

      wbResult.ActiveSheet.Rows(1).EntireRow.Delete

      Application.ScreenUpdating = True

      Application.DisplayAlerts = True

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2021-01-20T05:01:05+00:00

    Move the workbook creation out of the loop i.e.

    Set wbResult = Workbooks.Add

        FileName = Dir(FolderPath & "*.csv")

    Was this answer helpful?

    0 comments No comments
  5. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2021-01-20T04:55:35+00:00

    Was this answer helpful?

    0 comments No comments