Combine multiple tables of same format into one

deepakgoyal2005

Board Regular
Joined
Sep 1, 2008
Messages
58
I have an excel workbook with multiple sheets having data in Table format with exactly same columns. Need to combine data from all into 1 Table.

Sample:
Sheet "Sheet1" has a table under name "Table1" with 3 columns.
Sr. No.NameLogin ID
1Name 1Login 1
2Name 2Login 2
3Name 3Login 3
4Name 4Login 4
5Name 5Login 5

Similarly, Sheet "Sheet2" has a table under name "Table2" with same 3 columns.
Sr. No.NameLogin ID
6Name 6Login 6
7Name 7Login 7
8Name 8Login 8
9Name 9Login 9
10Name 10Login 10
11Name 11Login 11

And, likewise there could be more sheets added/removed in future.

There is another Sheet "SheetC" with similar table "TableC" where a combined output with data from other table needs to be created.

Trying to create a macro to loop through all the tables except the output combined table "TableC" and append the records in output "TableC". But I am facing trouble creating a loop. Tried googling through available samples, but they were not using data in table format, and hence were of little help.

Would really appreciate, if someone could help with their expertise. Thanks in advance.

VBA macro (WIP):

VBA Code:
Sub Combine_Tables()
'
' Consolidate data from Multiple tables
'
    Dim NextRow As Long  'To identify the last blank row in combined table
    
' Clear content in output combined table
    ActiveWorkbook.Sheets("Combine").Range("TableC").ClearContents
    
'Copy content from first "Table1"
    ActiveWorkbook.Sheets("Sheet 1").Select
    Range("Table1").Select
    Selection.Copy
    
    ActiveWorkbook.Sheets("Combine").Select
    Range("B3").Select
    Selection.End(xlDown).Select
    Range("B8").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Power Query solution: Make a connection (query) to each of the tables. Append queries.

Book1
ABCDEFG
1Sr. No.NameLogin IDSr. No.NameLogin ID
21Name 1Login 11Name 1Login 1
32Name 2Login 22Name 2Login 2
43Name 3Login 33Name 3Login 3
54Name 4Login 44Name 4Login 4
65Name 5Login 55Name 5Login 5
76Name 6Login 6
8Sr. No.NameLogin ID7Name 7Login 7
96Name 6Login 68Name 8Login 8
107Name 7Login 79Name 9Login 9
118Name 8Login 810Name 10Login 10
129Name 9Login 911Name 11Login 11
1310Name 10Login 10
1411Name 11Login 11
Sheet1
 
Upvote 0
Power Query solution: Make a connection (query) to each of the tables. Append queries.
Maybe you could show how, or even supply the M-code, as other members do.
 
Upvote 0
Sure. First I connected to one table (I started with Table2), and loaded it as a connection only (just to keep things tidy). It did the "Changed type" step automatically, and I was happy with the types it chose.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sr. No.", Int64.Type}, {"Name", type text}, {"Login ID", type text}})
in
    #"Changed Type"

Then I loaded Table1, and picked the Append Queries action. This table I loaded as a new table in cell E1.

1667484385194.png

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sr. No.", Int64.Type}, {"Name", type text}, {"Login ID", type text}}),
    #"Appended Query" = Table.Combine({#"Changed Type", Table2})
in
    #"Appended Query"
 
Upvote 0
I tried Power Query as an option, but there were 2 problems,

1) When new tables are added to the sheet, the query needs to be recreated or modified. In VBA, this cab be taken care by a loop eg. "IF "table name" <> "TableC", pick the data
2) Output is created in a new table, and cannot be created in an existing table, like "TableC" here in my example
 
Upvote 0
I tried Power Query as an option, but there were 2 problems,

1) When new tables are added to the sheet, the query needs to be recreated or modified. In VBA, this cab be taken care by a loop eg. "IF "table name" <> "TableC", pick the data
2) Output is created in a new table, and cannot be created in an existing table, like "TableC" here in my example
1) That can be solved with loading from a Folder instead of from specified, hard-coded tables. Here is a guide:
2) That is correct. Why do you need to load the data to an existing table?

In general, you can do things in VBA that are not (currently) possible in Power Query. But Power Query is much easier to learn.
 
Upvote 0
Thanks Automatrix, for spending time over my case. Appreciate.

Need the output in a specific table because I have a dashboard created over the output table, else it would be manual to transfer data from 1 to another.

You are right that power query is easier to learn, but doesn't seem to fit my requirement. I am trying to figure a solution in VBA and working on the code. I am hopeful on getting some help from the experts in this forum, like everytime.
 
Upvote 0
Thanks Automatrix, for spending time over my case. Appreciate.

Need the output in a specific table because I have a dashboard created over the output table, else it would be manual to transfer data from 1 to another.

You are right that power query is easier to learn, but doesn't seem to fit my requirement. I am trying to figure a solution in VBA and working on the code. I am hopeful on getting some help from the experts in this forum, like everytime.
Would it be possible to have the dashboard be based on the table generated by Power Query instead? You can also build the dashboard in Power BI, which is literally created to handle that sort of stuff.
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top