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, individual states 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 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.
How to merge excel files in a single excel spreadsheet 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.
Result: New Excel Sheet Opened containing combined date with a name "Combined"
Dim J As Integer
On Error Resume Next
Sheets(1).Name = "Combined"
For J = 2 To Sheets.Count
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
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: