In a continuation Excel VBA tutorial series of data manipulation using Macros; Today, we will try to merge excel files in a single excel spreadsheet. The source of the data may be from the same worksheet or from the closed workbook.
We will merge excel files into a single sheet. For example, an individual state’s data are placed on different sheets. What we need to do is we need to make it a master file. For Example, your data look like below.
There are almost 51 sheets in my sample workbook. Now if I give you this task to create a master file how long time will it take? Probably it will take almost 1 hour, right? And at the time it will be very tedious to do this as you need to create a new sheet and copy and paste the records in a new sheet for one by one.
But at the same time, a small piece of VBA code as in the below picture will give you immense benefit. For that, you don’t need to know how to write the VBA code. But you need to know where to place the VBA code and how to run VBA code.
How to Merge Excel Files in a Single Excel Spreadsheet Step by Step:
To merge Excel Files in Single Excel Spreadsheet, you have to follow below steps,
- Step 1: Press Alt + F11 to go to VBA page
- Step 2: From the Menu choose insert – Module
- Step 3: Copy and paste the below VBA code in the code window.
- Step 4: Press F5 to run the code.
[gdlr_core_code style="dark" ] Sub Combine() Dim J As Integer On Error Resume Next Sheets(1).Select Worksheets.Add Sheets(1).Name = "Combined" Sheets(2).Activate Range("A1").EntireRow.Select Selection.Copy Destination:=Sheets(1).Range("A1") For J = 2 To Sheets.Count Sheets(J).Activate Range("A1").Select Selection.CurrentRegion.Select Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2) Next End Sub [/gdlr_core_code] Result: New Excel Sheet Opened containing combined date with a name "Combined"
After running the code, you will see a new called “Combined” that has been created and all the data from the other sheet is now merged into the combined sheet. And you combine sheet will look like below
It will take not more than 10 seconds to complete the entire task. Isn’t that interesting? That’s why a small piece of code will help you to go back home on time.
In the very next topic, we will cover how to merge excel files from the closed workbook.
Till then please download the Excel macro file from here and play with it.