在不同的工作簿中将多个工作表合并为单个工作表

时间:2019-05-30 22:27:12

标签: excel vba excel-vba

我想将一个工作簿中多个工作表中的所有行和列复制到另一个工作簿中的单个工作表中。另外,我只想复制一次标题,即使它在我将复制的所有工作表中也是如此。

我可以打开包含要复制到目标工作表/工作簿的所有工作表的工作簿,但是,我不知道如何仅一次复制标题,并且经常会遇到“选择性粘贴”错误。

Sub Raw_Report_Import()

'Define variables'
Dim ws As Worksheet
Dim wsDest As Worksheet

'Set target destination'
Set wsDest = Sheets("Touchdown")

'For loop to copy all data except headers'
For Each ws In ActiveWorkbook.Sheets
    'Ensure worksheet name and destination tab do not have same name'
    If ws.Name <> wsDest.Name Then
        ws.Range("A2", ws.Range("A2").End(xlToRight).End(xlDown)).Copy
        wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End If
Next ws

End Sub

预期:第二个工作簿中的所有目标工作表都将被复制并粘贴到第一个工作簿中的目标工作表“ Touchdown”中,并且标头仅被复制一次。

实际:粘贴了一些值,但格式与原来的格式不符,并且排列不正确。

2 个答案:

答案 0 :(得分:0)

您的代码有很多错误。请找到以下代码(未经测试)。请注意区别,以便您可以改进。

请注意,设置目标工作表时,我将包括工作簿对象(如果在其他工作簿中)。这样可以防止发生错误。另请注意,此代码应在OLD工作簿中运行。另外,我假设您的标题位于每张纸的第1行中,因此我已经加入headerCnt来考虑这一点,并且仅将标题复制一次。

Option Explicit

Sub Raw_Report_Import()

    Dim ws As Worksheet
    Dim wsDest As Worksheet
    Dim lCol As Long, lRow As Long, lRowTarget As Long
    Dim headerCnt As Long

    'i would include the workbook object here
    Set wsDest = Workbooks("NewWorkbook.xlsx").Sheets("Touchdown")

    For Each ws In ThisWorkbook.Worksheets
        'this loops through ALL other sheets that do not have touch down name
        If ws.Name <> wsDest.Name Then
            'need to include counter to not include the header
            'establish the last row & column to copy
            lCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
            lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

            'establish the last row in target sheet
            lRowTarget = wsDest.Range("A" & wsDest.Rows.Count).End(xlUp).Row + 1

            If headerCnt = 0 Then
                'copy from Row 1
                ws.Range(ws.Cells(1, 1), ws.Cells(lRow, lCol)).Copy
            Else
                'copy from row 2
                ws.Range(ws.Cells(2, 1), ws.Cells(lRow, lCol)).Copy
            End If

            wsDest.Range("A" & lRowTarget).PasteSpecial xlPasteValues

            'clear clipboard
            Application.CutCopyMode = False
            'header cnt
            headerCnt = 1
        End If
   Next ws

End Sub

答案 1 :(得分:0)

尝试一下。

Sub CopyDataWithoutHeaders()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim shLast As Long
    Dim CopyRng As Range
    Dim StartRow As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Delete the sheet "RDBMergeSheet" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    'Add a worksheet with the name "RDBMergeSheet"
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "RDBMergeSheet"

    'Fill in the start row
    StartRow = 2

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> DestSh.Name Then

            'Find the last row with data on the DestSh and sh
            Last = LastRow(DestSh)
            shLast = LastRow(sh)

            'If sh is not empty and if the last row >= StartRow copy the CopyRng
            If shLast > 0 And shLast >= StartRow Then

                'Set the range that you want to copy
                Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))

                'Test if there enough rows in the DestSh to copy all the data
                If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                    MsgBox "There are not enough rows in the Destsh"
                    GoTo ExitTheSub
                End If

                'This example copies values/formats, if you only want to copy the
                'values or want to copy everything look below example 1 on this page
                CopyRng.Copy
                With DestSh.Cells(Last + 1, "A")
                    .PasteSpecial xlPasteValues
                    .PasteSpecial xlPasteFormats
                    Application.CutCopyMode = False
                End With

            End If

        End If
    Next

ExitTheSub:

    Application.Goto DestSh.Cells(1)

    'AutoFit the column width in the DestSh sheet
    DestSh.Columns.AutoFit

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

所有详细信息在这里。

https://www.rondebruin.nl/win/s3/win002.htm