简体   繁体   中英

Copy multiple rows from many sheets to one sheet

I receive a workbook daily that lists 50 rows of information per page on a variable number of pages depending on how many rows there are total.

How can I copy the 50 rows from each page onto one master list?

From recording a macro I get

Sub Macro2()  
    Sheets("Page1_2").Select
    Rows("5:54").Select
    Selection.Copy
    Sheets("Page1_1").Select
    Range("A56").Select
    ActiveSheet.Paste
End Sub

But I need it to loop through the entire workbook. I can't find a way to increment the sheet selection by 1 for each iteration and the paste range by 50.

Any help?

How about:

Sub test()

    Dim curRow As Integer
    Dim activeWorksheet As Worksheet
    Set activeWorksheet = ActiveSheet
    curRow = 1
    For Each ws In ActiveWorkbook.Worksheets
        If Not ws.Name = activeWorksheet.Name Then
            ws.Range("5:54").Copy Destination:=activeWorksheet.Range(CStr(curRow) & ":" & CStr(curRow + 49))

            curRow = curRow + 50
        End If
    Next ws

End Sub

It loops over all worksheets in the workbook and copies the contents to the current active sheet. The looping excludes the current active worksheet. It assumes that the contents that you are trying to aggregate are always in rows 5 through 54.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM