问题
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 different categories. Every table in the tab has the exact same columns. Is there a non-vbscript/marco way to create a master table that combines all of the tables into a single table in a different tab. This seems like it would be a no brainer but I have tried everything I can think of and find online and there doesn't seem to be a decent solution without an addon called power query.
回答1:
You can use the functionality of the pivottable wizard to consolidate multiple ranges (which are your tables) together into one pivottable.
When it prompts for you to add your ranges use the table names with the following syntax: Table4[#All]
You need the [#All] to get all the data associated with the table. Just repeat this for each of your tables names you want to consolidate.
Full description i have given in my answer here:
combining data from two sheets and generating pivot table in another sheet
Note: If you want to keep the original table names or table numbers you will need to select the option:
1) "I will create the Page Fields"
2) Enter the Ranges using the table name e.g. Table4[#All]
3) Select how many page fields do you want 1-4 and add item label used to identify the selected ranges below e.g. Table4.
I am not sure if 4 items is the maximum or if this can be extended through VBA. However you can also use PowerQuery or UnionQuery.
The following quotes are from here: http://www.contextures.com/xlPivot08.html
I include some outline in case links are lost.
PowerQuery:
If you have a version of Excel that supports Microsoft's Power Query add-in, you can use it to combine the data in two or more tables. The tables can be in the same workbook, or in different files.
http://www.contextures.com/xlPivot08.html#videopowerquery
Union Query:
If you can't combine your data on a single worksheet, another solution is to create named ranges in an Excel file, and use Microsoft Query (MS Query) to combine the data.
http://www.contextures.com/xlPivot08.html#union01
回答2:
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:
回答3:
If you have Excel 2013 or later, then this is the perfect excuse to go play with PowerQuery, which is now called 'Get & Transform' in the ribbon. You can see something very similar to your requirement in my answer at excel indirect function to read dates and return dynamic values
I strongly suggest you go and look at that thread...even if for some reason you can't use PowerQuery for this particular challenge, because I reckon it's worth seeing just how simple it is to mash together identical tables using PowerQuery, even if just for future reference. It's basically a VBA developer in a box. It's a VBA gimp!
来源:https://stackoverflow.com/questions/47277206/combine-multiple-tables-rows-into-master-table