Merge CSV Files into Multiple Sheets in Excel (with Easy Steps)

 

We have three individual Excel workbooks for grade 6, grade 7, and grade 8 report cards for various subjects for multiple students. The files are in CSV format with the students’ names, subjects, and scores. We want to merge all the workbooks in a single workbook in multiple sheets.

Sample Dataset to Merge CSV Files into Multiple Sheets


Step 1 – Open a VBA Module

  • Go to the Developer tab and select Visual Basic.

Access the Visual Basic Tool

  • The VBA Editor window will open.
  • Go to the Insert tab and choose the Module option.

Insert a New Module to Merge CSV Files into Multiple Sheets

Read More: How to Keep Leading Zeros in Excel CSV Programmatically


Step 2 – Insert the VBA Code

  • Click on Module 1.
  • Insert the following code in window.
Sub MergeCSVFiles()
    Dim openFiles As Variant
    Dim I As Integer
    Dim wb As Workbook
    Dim mergedWb As Workbook
    Dim delimiter As String
    Dim screen As Boolean
    On Error GoTo ErrHandler
    screen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    delimiter = ";"
    openFiles = Application.GetOpenFilename("CSV Files (*.csv), *.csv", , "Merge CSV Files", , True)
    If TypeName(openFiles) = "Boolean" Then
        MsgBox "No files were selected", , "Merge CSV Files"
        GoTo ExitHandler
    End If
    I = 1
    Set mergedWb = Workbooks.Open(openFiles(I))
    mergedWb.Sheets(1).Copy
    Set wb = Application.ActiveWorkbook
    mergedWb.Close False
    Do While I < UBound(openFiles)
        I = I + 1
        Set mergedWb = Workbooks.Open(openFiles(I))
        mergedWb.Sheets(1).Move , wb.Sheets(wb.Sheets.Count)
    Loop
ExitHandler:
    Application.ScreenUpdating = screen
    Set wb = Nothing
    Set mergedWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, , "Merge CSV Files"
    Resume ExitHandler
End Sub
  • Press Ctrl + S on your keyboard to save the VBA code.

Write and Save the Required VBA Code to Merge CSV Files into Multiple Sheets

  • A Microsoft Excel dialogue box will appear.
  • Click on the No option.

Microsoft Excel Dialogue Box

  • The Save As window will appear.
  • Change the Save as type: option as .xlsm format and click on the Save button.

Save the Excel File as .xlsm File to Merge CSV Files into Multiple Sheets

Read More: How to Fix CSV File in Excel


Step 3 – Run the Code to Merge CSV Files into Multiple Sheets

  • Go to the Developer tab and choose Macros.

Access the Macros Tool

  • The Macro window will appear.
  • Choose the MergeCSVFiles macro from the Macros pane and click on the Run button.

Run Macro to Merge CSV Files into Multiple Sheets

  • All the workbooks will be merged into a new workbook as different worksheets.

Merged CSV Files into Multiple Sheets

  • Go to the File tab.

Access the File Tab

  • Click on the Save As option.

Choose the Save As Option

  • Click on the Browse option.

Excel Save As Window

  • The Save As dialog box will appear.
  • Insert your desired file name in the File name: text box and click on the Save button.

Save As Dialogue Box

  • The CSV files are merged into multiple sheets of a single workbook.

Merged CSV Files into Multiple Sheets

Read More: Paste Comma Separated Values into Excel


Use Power Query to Merge CSV Files into a Single Sheet in Excel

Steps:

  • Go to the Data tab and select Get Data.
  • Choose From File and select the From Folder option.

Access the Power Query Tool to Merge CSV Files into a Single Sheet

  • The Browse window will open.
  • Choose the folder where you have kept the CSV files.
  • Click on the Open button.

Choose Folder Containing CSV Files

  • The Power Query window will open and show the CSV files to import.
  • Click on Combine and select the Combine & Transform Data option.

Choose the Combine & Transform Data Option to Merge CSV Files into a Single Sheet

  • The Combine Files window will appear.
  • Choose the Delimiter as Semicolon (or change depending on what your data is delimited by).
  • Click on OK.

Combine Files Window

  • You will get the merged file in the Power Query window.

Merged CSV Files in Power Query Window

  • Go to the File tab from the Power Query window.
  • Click on Close & Load and select Close & Load.

Choose the Close & Load Option to Merge CSV Files into a Single Sheet

  • You will get all the CSV files merged into a single sheet in Excel. The output should look like this.

Merged CSV Files into a Single Sheet

Read More: How to Edit CSV File in Excel


Things to Remember

  • Maintain a specific delimiter for every file.
  • All CSV files should be oriented in the same way and from the same rows and columns.

Download the Practice Workbook

CSV Source Files:

Final Merged File:


Related Articles


<< Go Back to Merge CSV Files in Excel | Import CSV to Excel | Importing Data in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Tanjim Reza
Tanjim Reza

Md. Tanjim Reza Tanim, a BUET graduate in Naval Architecture & Marine Engineering, contributed over one and a half years to the ExcelDemy project. As an Excel & VBA Content Developer, he authored 100+ articles and, as Team Leader, reviewed 150+ articles. Tanim, leading research, ensures top-notch content on MS Excel features, formulas, solutions, tips, and tricks. His expertise spans Microsoft Office Suites, Automating Finance Templates, VBA, Python, and Developing Excel Applications, showcasing a multifaceted commitment to the... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo