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
 
Try:
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim srcWB As Workbook
    For Each srcWB In Application.Workbooks
        If srcWB.Name <> "Master.xlsm" Then
            LastRow = srcWB.Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            srcWB.Sheets("Sheet1").Rows("2:" & LastRow).EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next srcWB
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I'm not following how you're last piece of code should fit with the previous code. I pasted where I thought it fit and it didn't change anything. Still not looping.
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim srcWB As Workbook
    For Each srcWB In Application.Workbooks
       If srcWB.Name <> "Master.xlsm" Then
          LastRow = srcWB.Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            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
 
Upvote 0
Still not looping. I pasted the code how where I thought it would go. still only pulls the data one time.
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    Dim srcWB As Workbook
    For Each srcWB In Application.Workbooks
       If srcWB.Name <> "Master.xlsm" Then
          LastRow = srcWB.Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            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
Have you tried the macro I posted in Post #11 ?
 
Upvote 0
I did. I think I have something that will work for me. Since I know mathematically how many times to loop the code I did the following:
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim srcWB As Workbook
    Dim i As Integer
    i = 0
    Do Until i = 10
    For Each srcWB In Application.Workbooks
       If srcWB.Name <> "Master.xlsm" Then
            Select Case srcWB.Name
                Case "code1", "code3.xlsx", "code5.xlsx"
                    srcWB.Sheets("Sheet1").Rows("2:51").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                    srcWB.Sheets("Sheet1").Rows("2:51").Delete
                Case "code2.xlsx"
                    srcWB.Sheets("Sheet1").Rows("2:64").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                    srcWB.Sheets("Sheet1").Rows("2:64").Delete
                Case "code5.xlsx"
                    srcWB.Sheets("Sheet1").Rows("2:39").EntireRow.Copy Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                    srcWB.Sheets("Sheet1").Rows("2:39").Delete
            End Select
        End If
    Next srcWB
    i = i + 1
    Debug.Print i
    Loop
    Application.ScreenUpdating = True
End Sub

Thoughts??
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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