In this blog article, we will be covering how to loop through multiple sheets and merge data as per our requirement. Just imagine you have a lot of files in a folder and for each file, you have 3 sheets.
Let me explain you how to merge data with an example.
Now If I ask you to prepare a final master file which will be a combination of all the files and all the sheets (Merge Data). Making you clearer on this. In my data folder, I have two files, as you can see in the below screenshot.
When I open a file I get 3 sheets like 1, 2 and 3… (Refer screenshot below) for all the files, It’s same in all the spreadsheets. Now I have another file called Master where I need to merge all the sheets. Not in single sheets but for Master sheet also I have 3 sheets as the heading is not the same for all the sheets.
Now if you are asked to prepare a master file where all you need to do is you will have to do the following manually.
Open your master file:
- Go to the folder and open the first file.
- Then go to the first sheet of the file and copy the data.
- Go to your master file and then select the First sheet.
- Finally, paste the data.
Similarly, you will have to do the same thing for the other file also. Now think of that situation where there are 100 files in your folder and the 50 sheets are there in each file. If I take 30 secs to copy and paste, then it will take 30 * 50 = 1500 secs for a single file and for 100 files ??? Oh!!! It is too boring!!!
But if you know the code it will take few minutes to complete the job and the most important thing is that you don’t have to do anything. Your code will do the trick for you to merge data in the master file.
Here are the steps to do it automatically:
- Open Master File.
- Put the header of all the sheets.
- Press Alt + F11 to go the VBA page.
- Go to insert and then Module.
- Copy the below code and then paste in the Module.
- Finally, run the code by pressing F5.
Sub MergeWbooksAndSheets() Dim wbk As Workbook Dim sht As Worksheet Dim shtt As Worksheet Dim sheetfirst As Worksheet Dim sheetsecond As Worksheet Dim sheetthird As Worksheet Dim wbk2 As Workbook Set wbk2 = ThisWorkbook Dim Filename As String Dim Path As String Path = "C:\Personal\Data\" 'CHANGE Folder PATH Filename = Dir(Path & "*.xlsx") 'OPEN EXCEL FILES Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN Set wbk = Workbooks.Open(Path & Filename) wbk.Activate For Each shtt In wbk.Worksheets wbk.Activate Var = shtt.Name shtt.Select Range("A2").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy Windows("Master.xlsm").Activate Dim lr As Integer 'Var = sht.Name Sheets(Var).Select lr = wbk2.Sheets(Var).Cells(Rows.Count, 1).End(xlUp).Row Cells(lr + 1, 1).Select ActiveSheet.Paste Next wbk.Close True Filename = Dir Loop End Sub
Your VBA code will look like below:
You will notice that the code will go to that directory, open the excel file and copying and paste the data one by one from the sheets.
Further, we have already covered the blog on How to Merge Excel Files into a Spreadsheet using Excel Macro
Thanks for your valuable feedback which motivates us to write useful blogs on new topics.