Join Tables

22strider

Active Member
Joined
Jun 11, 2007
Messages
302
Hello Friends

Could you please help me with VBA Code for joining two tables on a common column and then generate a third table. Both the tables are on a separate worksheet and the result table needs to be on a separate sheet as well. I can easily do this using Access but my requirement is to use Excel. I am using Excel 2007.
Following are the tabls showing sample data; Table1 and Table2 are the source tables having Manufacturer P/N as the common column. And Result Table is showing how the joined table should look like.
Thanks for your help
Rajesh


<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;"></td><td style=";">Table1</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">P/N</td><td style=";">Manufacturer P/N</td><td style=";">Manufacturer Name</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">500-123</td><td style=";">700-ABC</td><td style=";">ABC</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">500-123</td><td style=";">700-DEF</td><td style=";">DEF</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style=";">500-546</td><td style=";">600-XYZ</td><td style=";">XYZ</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style=";">500-546</td><td style=";">600-PQR</td><td style=";">PQR</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style=";">400-123</td><td style=";">700-ABC</td><td style=";">ABC</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="text-align: right;;"></td><td style=";">Table2</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style=";">Manufacturer P/N</td><td style=";">Manufacturer Name</td><td style=";">Country</td><td style=";">Type</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style=";">700-ABC</td><td style=";">ABC</td><td style=";">USA</td><td style=";">Supplier</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style=";">700-DEF</td><td style=";">DEF</td><td style=";">China</td><td style=";">Manufact.</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style=";">600-XYZ</td><td style=";">XYZ</td><td style=";">India</td><td style=";">Supplier</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style=";">600-PQR</td><td style=";">PQR</td><td style=";">USA</td><td style=";">Designer</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">17</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">18</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">19</td><td style="text-align: right;;"></td><td style=";">Result Table</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">20</td><td style=";">P/N</td><td style=";">Manufacturer P/N</td><td style=";">Manufacturer Name</td><td style=";">Address</td><td style=";">Type</td></tr><tr ><td style="color: #161120;text-align: center;">21</td><td style=";">500-123</td><td style=";">700-ABC</td><td style=";">ABC</td><td style=";">USA</td><td style=";">Supplier</td></tr><tr ><td style="color: #161120;text-align: center;">22</td><td style=";">500-123</td><td style=";">700-DEF</td><td style=";">DEF</td><td style=";">China</td><td style=";">Manufact.</td></tr><tr ><td style="color: #161120;text-align: center;">23</td><td style=";">500-546</td><td style=";">600-XYZ</td><td style=";">XYZ</td><td style=";">India</td><td style=";">Supplier</td></tr><tr ><td style="color: #161120;text-align: center;">24</td><td style=";">500-546</td><td style=";">600-PQR</td><td style=";">PQR</td><td style=";">USA</td><td style=";">Designer</td></tr><tr ><td style="color: #161120;text-align: center;">25</td><td style=";">400-123</td><td style=";">700-ABC</td><td style=";">ABC</td><td style=";">USA</td><td style=";">Supplier</td></tr><tr ><td style="color: #161120;text-align: center;">26</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet1</p><br /><br />
 

ExcelTheCell

Board Regular
Joined
Nov 14, 2010
Messages
158
And this can be placed in worksheet 1 if you want that it works on change

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'If change in first worksheet has filled all 3 fields then call macro Joiner
Lastcell = WorksheetFunction.CountA(Columns(1))
If Range("A" & Lastcell).Offset(0, 1).Value = "" Then Exit Sub
If Range("A" & Lastcell).Offset(0, 2).Value = "" Then Exit Sub
Call Joiner
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

RonEmm

Board Regular
Joined
Aug 18, 2009
Messages
108
Rajesh,

I checked again on my side and the code works fine. It could be the names and/or ranges in your workbook. Otherwise post a sample of what goes wrong.
 

Forum statistics

Threads
1,137,351
Messages
5,680,988
Members
419,948
Latest member
Sbakker1

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
Top