Have you ever found yourself needing to combine multiple Excel files into one? It can be a tedious and time-consuming process if done manually. But fear not, as there are several ways to merge Excel files quickly and efficiently.
Merge Excel Files Using Power Query
One of the easiest ways to merge Excel files is by using Power Query. Power Query is a free add-in for Excel that allows you to import, transform, and combine data from multiple sources.
Follow these steps to merge Excel files using Power Query:
- Open a new Excel workbook.
- Click on the “Data” tab and then click on “From File” in the “Get External Data” section.
- Select “From Workbook” and navigate to the folder containing the Excel files you want to merge.
- Select the first Excel file and click “Open.”
- In the “Navigator” pane, select the worksheet you want to merge.
- Click on the “Edit” button to open the Power Query Editor.
- Click on the “Add Column” tab and select “Custom Column.”
- In the formula bar, enter the following formula:
- Click on “OK.”
- Expand the new column by clicking on the double-arrow icon.
- Select the columns you want to include in the merged table.
- Click on “Close & Load” to merge the Excel files into one table.
= Excel.Workbook(File.Contents([Folder Path] & [File Name]), null, true)
Merge Excel Files Using VBA Code
If you’re comfortable working with VBA code, you can also merge Excel files using a macro. Here’s an example of VBA code that merges all Excel files in a folder:
Sub MergeExcelFiles()
Dim path As String, fileName As String, wsName As String
Dim wb As Workbook, wbNew As Workbook
Dim ws As Worksheet, wsNew As Worksheet
Dim lastRow As Long
' Set path to folder containing Excel files to merge
path = "C:\Folder Name\"
' Create new workbook to hold merged data
Set wbNew = Workbooks.Add
' Loop through each Excel file in folder
fileName = Dir(path & "*.xlsx")
Do While fileName <> ""
' Open Excel file
Set wb = Workbooks.Open(Filename:=path & fileName)
' Loop through each worksheet in Excel file
For Each ws In wb.Worksheets
' Copy data to new worksheet in new workbook
Set wsNew = wbNew.Worksheets.Add
ws.UsedRange.Copy wsNew.Range("A1")
wsNew.Name = wb.Name & " " & ws.Name
' Find last row in new worksheet and add hyperlink to original worksheet
lastRow = wsNew.Cells(wsNew.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
wsNew.Hyperlinks.Add Anchor:=wsNew.Cells(i, 1), _
Address:="", _
SubAddress:="'" & ws.Name & "'!" & wsNew.Cells(i, 1).Address, _
TextToDisplay:=wsNew.Cells(i, 1).Value
Next i
Next ws
' Close Excel file
wb.Close SaveChanges:=False
' Find next Excel file in folder
fileName = Dir
Loop
End Sub
FAQs
1. Can I merge multiple sheets within the same Excel file?
Yes, you can merge multiple sheets within the same Excel file by using the “Consolidate” function. Here’s how:
- Select the cell where you want the consolidated data to be placed.
- Click on the “Data” tab and then click on “Consolidate” in the “Data Tools” section.
- In the “Function” section, select the function you want to use to consolidate the data (e.g. “Sum,” “Average,” etc.).
- In the “Reference” section, select the range of cells you want to consolidate.
- In the “Location” section, select “Top Row” or “Left Column” depending on how your data is arranged.
- Click on “OK” to consolidate the data.
2. Can I merge Excel files with different headers and columns?
Yes, you can merge Excel files with different headers and columns by using Power Query. Power Query allows you to transform and reshape data as needed, so you can easily combine files with different structures.
Watch How to Merge Excel Files
Prefer watching to reading? Check out this video on how to merge Excel files using Power Query.