Copying Excel data from multiple worksheets into one single sheet

前端 未结 4 1920
囚心锁ツ
囚心锁ツ 2021-01-16 13:26

I have tried searching the internet for various answers to this question but cannot find the right answer. I have an Excel Workbook with worksheets represent each day of the

4条回答
  •  花落未央
    2021-01-16 13:43

    MASSIVE EDIT:

    As with last chat with Iain, the correct parameters have been set. I have removed the last few code snippets as they are quite not right. If anyone is still interested, please check the edit history.

    Hopefully, this is the final edit. ;)

    So, the correct conditions needed are:

    1. Month name in sheet. We used an Input Box for this.
    2. We check for number of rows. There are three conditions: 157 rows total, 41 rows total, and all else.

    The following subroutine will do the trick.

    Sub BlackwoodTransfer()
    
        Dim Summ As Worksheet, Ws As Worksheet
        Dim ShName As String
        Dim nRow As Long
    
        Set Summ = ThisWorkbook.Sheets("Summary")
        ShName = InputBox("Enter month for Call Flow in mmmm format (ie. November, etc.):") & " Call Flow"
        'Returns November Call Flow. This means it will target every sheet that has November Call Flow in its name.
    
        Application.ScreenUpdating = False
    
        For Each Ws In ThisWorkbook.Worksheets
            If InStr(1, Ws.Name, ShName) > 0 Then
            'Starting from first character of the sheet's name, if it has November, then...
                nRow = Summ.Cells(Rows.Count, 1).End(xlUp).Row + 1
                '... get the next empty row of the Summary sheet...
                Select Case Ws.Cells(Rows.Count, 1).End(xlUp).Row
                '... check how many rows this qualified sheet has...
                    Case 157
                    '... if there are 157 rows total...
                        Ws.Range(Cells(57,1),Cells(104,13)).Copy
                        '... copy Rows 57 to 104, 13 columns wide...
                        Summ.Range("A" & nRow).PasteSpecial xlPasteAll
                        '... and paste to next empty row in Summary sheet.
                    Case 41
                        Ws.Range(Cells(23,1),Cells(126,13)).Copy
                        Summ.Range("A" & nRow).PasteSpecial xlPasteAll               
                    Case Else
                        Ws.Range(Cells(23,1),Cells(30,13)).Copy
                        Summ.Range("A" & nRow).PasteSpecial xlPasteAll
                End Select
            End If
        Next Ws
    
        Application.ScreenUpdating = True
    
    End Sub
    

    @Iain: Check out the comments and cross reference them with the MSDN database. That should explain what each function/method is doing exactly. Hope this helps!

提交回复
热议问题