Macro to Merge tables 1:M relationship

Gimics

Board Regular
Joined
Jan 29, 2014
Messages
164
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am looking for some help to combine two tables of separate data based on a single column with a 1:M relationship. I do not have power pivot, so I am defaulting to vba as the best option (unless I hear otherwise).

Example:

The two tables share Group as a common Key - the macro should create a new table, joining both on group and extending to include both values. I do have other columns of data in each table, but they would be simple additions to the result and do not affect the join.

Table 1
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD]Account[/TD]
[TD]Group[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Plum[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Plum[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Pear[/TD]
[/TR]
</tbody>[/TABLE]


Table 2
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD]Group[/TD]
[TD]Center[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]Apple[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]Plum[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]Plum[/TD]
[TD]2500[/TD]
[/TR]
[TR]
[TD]Plum[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]Pear[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]Pear[/TD]
[TD]2500[/TD]
[/TR]
[TR]
[TD]Pear[/TD]
[TD]3000[/TD]
[/TR]
</tbody>[/TABLE]


Expected result:
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD]Account[/TD]
[TD]Group[/TD]
[TD]Center[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Apple[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Apple[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Apple[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Plum[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Plum[/TD]
[TD]2500[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Plum[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Pear[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Pear[/TD]
[TD]2500[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]Pear[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Apple[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Apple[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Apple[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Plum[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Plum[/TD]
[TD]2500[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Plum[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Pear[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Pear[/TD]
[TD]2500[/TD]
[/TR]
[TR]
[TD]60000[/TD]
[TD]Pear[/TD]
[TD]3000[/TD]
[/TR]
</tbody>[/TABLE]


Thanks in advance!
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Are your tables real Excel Table objects or just ranges of cells that you think of as a table? If the former, are the table names really Table1 and Table2? If the latter, what is the addresses of your two table ranges?
 
Upvote 0
Are your tables real Excel Table objects or just ranges of cells that you think of as a table? If the former, are the table names really Table1 and Table2? If the latter, what is the addresses of your two table ranges?

Hey Rick - they are real table objects with table names "Table1" and "Table2". If it's easier to use the range addresses, they both start at 1,1 of two separate worksheets (Sheet1 and Sheet2).

Thanks
 
Upvote 0
Anyone with any insights? I was thinking of using "for" loops with 'find' lookups and storing the found ranges then cycling through everything, for each item in the original table... but there's gotta be a simpler way!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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