Combine Multiple Tables Rows Into Master Table

后端 未结 3 1805
花落未央
花落未央 2020-11-29 11:07

Happy Monday Everyone!

Have a question and hope you can help. I have a budget spreadsheet that has a budget tab. On this tab is about 8 tables broken down into diffe

3条回答
  •  长情又很酷
    2020-11-29 11:22

    I know you asked for a non VBA way, but for completeness I'm adding another answer that also has a VBA solution, because it's dead simple, it's blazingly fast, and it's generic. All you need to do is cut and paste this code into a standard code module, add a button and assign it to trigger the calling routine, give your source tables a name includes the full name of the summary table, and you're good to go.

    Sub CombineTables(loDest As ListObject, Optional lcSource As ListColumn)
    
    Dim ws              As Worksheet
    Dim lo              As ListObject
    Dim lc              As ListColumn
    Dim rDest           As Range
    Dim lDestRows       As Long
    Dim lSourceRows     As Long
    
    Application.ScreenUpdating = False
    
    If lcSource Is Nothing Then Set lcSource = loDest.ListColumns(1)
    If loDest.ListRows.Count > 0 Then loDest.DataBodyRange.Delete
    
    For Each ws In ActiveWorkbook.Worksheets
        For Each lo In ws.ListObjects
            If lo <> loDest Then
                With lo
                    If InStr(.Name, loDest.Name & "_") > 0 Then
                        On Error Resume Next
                        lDestRows = loDest.ListRows.Count
                        On Error GoTo 0
                        lSourceRows = .ListRows.Count
                        If lSourceRows > 0 Then
    
                            'Work out where we want to paste the data to
                            Set rDest = loDest.HeaderRowRange.Offset(1 + lDestRows).Resize(lSourceRows)
    
                            'Resize the destination table
                            loDest.Resize loDest.Range.Resize(1 + lSourceRows + lDestRows)       
    
                            For Each lc In .ListColumns
                             Intersect(loDest.ListColumns(lc.Name).Range.EntireColumn, rDest).Value2 = lc.DataBodyRange.Value
                            Next lc
                            Set lc = Nothing
                            On Error Resume Next
                            Set lc = .ListColumns(lcSource.Name)
                            On Error GoTo 0
                            If lc Is Nothing Then Intersect(lcSource.Range, rDest.EntireRow).Value2 = ws.Name
                        End If
                    End If
                End With
            End If
        Next lo
    Next ws
    
    Application.ScreenUpdating = True
    
    End Sub
    

    And here's the caller:

    Sub CombineTables_Caller()
    CombineTables [SomeName].ListObject, [SomeName].ListObject.ListColumns("Source")
    End Sub
    

    When I push that button, the code will look throughout the workbook for any tables who's names contain the name of the Destination table (in this case the Table called "SomeName"), and then bring their data through. So if you are adding new tabes, then as long as you prefix their Table names with the name of the destination table, they will be included. Any other tables (such as the one called 'DifferentName' will be ignored.

    ...and here's the result:

提交回复
热议问题