通过VBE将源工作表动态合并到相应的目标工作表

时间:2018-08-17 16:03:20

标签: excel merge worksheet

我正在尝试将源工作簿中的所有工作表合并到相应的“主”工作表(相同的工作表名称,相同的列名),再合并到主(目标)工作簿。每当找到一个新的“源”工作簿时,就会调用下面的子例程(单独的无关代码不包含在帖子中)。

我被困在如何访问两张纸上,因此我可以进行复印。我已经编写了代码,但是由于我无法访问目标表,因此它是不正确的。

我在评论中指出我目前仍停留在哪里。其他DestSh代码也可能被错误地引用,但我还没有得到那么远。

***** *****

下面的子例程
Sub MergeWorkbookToMaster(ByRef MasterWorkbook As Workbook, ByVal SourceWorkbook As Workbook)

  'Merge all Worksheets from passed in Workbook and merge them into the MasterWorkbook (also passed in) to there respective corresponding sheets. Do not know how many sheets there are, but each of the individual sheets have the same header rows (2) and the same identical number of columns (for each sheet).

    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim shLast As Long
    Dim CopyRng As Range
    Dim StartRow As Long

    'Fill in the start row
    StartRow = 3

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In SourceWorkbook.Worksheets
        If sh.Name <> DestSh.Name Then  'Stuck here - Need help with this line

            '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 'Get Next Sheet

ExitTheSub:

    Application.Goto DestSh.Cells(1)

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

    Debug.Print "loop through all worksheets and copy the data to the DestSh"

End Sub 

'Common Functions required for all routines
Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function

Function LastCol(sh As Worksheet)
    On Error Resume Next
    LastCol = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
    On Error GoTo 0
End Function

0 个答案:

没有答案
相关问题