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.
Let’s learn how to merge excel files in a single excel with an example:
We will merge excel files into a single sheet. For example, individual states data are placed in 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 new sheet for one by one.
But at the same time, a small piece of VBA code as in 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.
Here is the detailed process step by step:
- Press Alt + F11 to go to VBA page
- From the Menu choose insert – Module
- Copy and paste the below VBA code in the code window.
- Press F5 to run the code.
Copy VBA Code for merge excel files from below:
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
After running the code, you will see a new called “Combined” 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 tasks. 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.
If you are regularly following our blogs you will notice that we are trying to cover a lot of things to automate the process through VBA. Please follow the below links:
Looking for Fre excel macro tutorial to get started? <– Click Here