Merge spreadsheets by different row quantity

snapz

New Member
Joined
Nov 2, 2017
Messages
9
I have 5 workbooks. We'll call them code1, code2, code3, code4, code5. I need to merge these into one workbook called master. All workbooks only have one sheet named Sheet1. I need to pull 50 records (rows) from code1, 63 records (rows) from code2, 50 records (rows) from code3, 38 records (rows) from code4, 50 records (rows) from code5 and repeat the process until all the records are merged. Is this even possible?

Thanks. TJ
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Are all the workbooks already open or are they closed? Does the sheet in the source workbooks have a header in row 1? If it does, do you want the header to be copied followed by the number of set rows? Do the 5 workbooks have an "xlsx" extension?
 
Upvote 0
Workbooks can be open or closed. Whichever is easier. Right now they are closed. Each sheet has a header row. It doesn't need to be copied, I can delete it. Yes they have an xlsx extension. Also each workbook has 3 columns a,b,c.
 
Upvote 0
Copy/paste this macro into a regular code module in your "Master" workbook. I am assuming that your destination sheet in the Master is named "Sheet1". Save the Master as a macro-enabled file so its extension changes to "xlsm". Make sure that the other workbooks are all open. You don't have to delete the header rows. Change the file names in the code to match your actual file names.
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim srcWB As Workbook
    For Each srcWB In Application.Workbooks
        If srcWB.Name <> "Master.xlsm" Then
            Select Case srcWB.Name
                Case "code1.xlsx", "code3.xlsx", "code5.xlsx"
                    srcWB.Sheets("Sheet1").Rows("2:51").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                Case "code2.xlsx"
                    srcWB.Sheets("Sheet1").Rows("2:64").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                Case "code4.xlsx"
                    srcWB.Sheets("Sheet1").Rows("2:39").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            End Select
        End If
    Next srcWB
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Named all my workbooks accordingly. Ran the macro. Get a run-time error '9': script out of range. Debugger highlights

srcWB.Sheets("Sheet1").Rows("2:51").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

under the Case "code1.xlsx", "code3.xlsx", "code5.xlsx"
 
Upvote 0
Nevermind. I had a copy/paste failure. The macro works. Sort of. It doesn't keep repeating the macro until the end of the workbooks. It only gives me one set. Is there a way to make it repeat until the end of the workbooks?
 
Upvote 0
I tried it on some dummy workbooks and it worked properly. Are they all open? Check that the names are correct.
 
Upvote 0
It did work. It was my error. Now I need the macro to loop until it gets to the first empty row of code1.xlsx (or any workbook if it's easier that way)
 
Upvote 0
Do you mean you want to copy all the rows in each of the 5 workbooks up to the first empty row?
 
Upvote 0
Yes. Currently my test documents have enough data to loop the macro 10 times. My project will be considerably more. I'd like the macro to loop until it gets all of the data into the master document.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top