Hello,
I have two tables that are maintained separately, but share common elements. Based on those common elements, I would like to merge the tables and extend them into a full data list.
The first table has headings across the top (let's call them location groups) that need to be looked up in the second table, which will contain multiple records (let's called them locations) of those headings associated with additional fields. After looking the value up, I want to create a third table that's a list of all of the source data and looked up values. Much easier to explain with visuals:
Table 1(source data):
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Fruit Types[/TD]
[TD="align: center"]Stores[/TD]
[TD="align: center"]Ecommerce[/TD]
[TD="align: center"]Corporate[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Gala[/TD]
[TD]Fuji[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Mandarin[/TD]
[TD]Madarin[/TD]
[/TR]
</tbody>[/TABLE]
Table 2(mapping table):
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Location Groups[/TD]
[TD]Locations[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Stores[/TD]
[TD]Store 200[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Stores[/TD]
[TD]Store 300[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Ecommerce[/TD]
[TD]USA Website[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Ecommerce[/TD]
[TD]Global Website[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
</tbody>[/TABLE]
Table 3(output; for each fruit type and fruit variety, lookup location group and create a record for each location):
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]Fruit Types[/TD]
[TD="align: center"]Fruit Variety[/TD]
[TD="align: center"]Location Group[/TD]
[TD="align: center"]Location[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 200[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 300[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]USA Website[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]Global Website[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Apple[/TD]
[TD]Fuji[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 200[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 300[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]USA Website[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]Global Website[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
</tbody>[/TABLE]
I have many more columns and rows of data, but this basic layout would solve my problem. I know how to look through the source table and then how to update the output table with those values, but I don't know how to find multiple values in the mapping table and return multiple values.
Say I've declared variables for the source table (Table1) and mapping table (Table2), including their rows, columns and data, and selected an output range for Table3 as a single cell (outRng); this would roughly be where I'm at (the code below isn't complete for all of the headings, but I can figure that part out...this is just for the mapping lookup):
This, obviously, only returns one value, as range.find only returns the range of the first found cell. This would be my resulting table:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD="align: center"]Fruit Types[/TD]
[TD="align: center"]Fruit Variety[/TD]
[TD="align: center"]Location Group[/TD]
[TD="align: center"]Location[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Apple[/TD]
[TD]Fuji[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
</tbody>[/TABLE]
What am I missing? It would be great if I could use a range.resize(foundrange.size) = foundrange.offset(1) kind of formula here, but maybe I can only do this with multiple loops?
Thanks in advance!
I have two tables that are maintained separately, but share common elements. Based on those common elements, I would like to merge the tables and extend them into a full data list.
The first table has headings across the top (let's call them location groups) that need to be looked up in the second table, which will contain multiple records (let's called them locations) of those headings associated with additional fields. After looking the value up, I want to create a third table that's a list of all of the source data and looked up values. Much easier to explain with visuals:
Table 1(source data):
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Fruit Types[/TD]
[TD="align: center"]Stores[/TD]
[TD="align: center"]Ecommerce[/TD]
[TD="align: center"]Corporate[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Gala[/TD]
[TD]Fuji[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Mandarin[/TD]
[TD]Madarin[/TD]
[/TR]
</tbody>[/TABLE]
Table 2(mapping table):
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Location Groups[/TD]
[TD]Locations[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Stores[/TD]
[TD]Store 200[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Stores[/TD]
[TD]Store 300[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Ecommerce[/TD]
[TD]USA Website[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Ecommerce[/TD]
[TD]Global Website[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
</tbody>[/TABLE]
Table 3(output; for each fruit type and fruit variety, lookup location group and create a record for each location):
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]Fruit Types[/TD]
[TD="align: center"]Fruit Variety[/TD]
[TD="align: center"]Location Group[/TD]
[TD="align: center"]Location[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 200[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 300[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]USA Website[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]Global Website[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Apple[/TD]
[TD]Fuji[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 200[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 300[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]USA Website[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]Global Website[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
</tbody>[/TABLE]
I have many more columns and rows of data, but this basic layout would solve my problem. I know how to look through the source table and then how to update the output table with those values, but I don't know how to find multiple values in the mapping table and return multiple values.
Say I've declared variables for the source table (Table1) and mapping table (Table2), including their rows, columns and data, and selected an output range for Table3 as a single cell (outRng); this would roughly be where I'm at (the code below isn't complete for all of the headings, but I can figure that part out...this is just for the mapping lookup):
Code:
For i = 1 to Table1.rows.count
For j = 1 To Table1.columns.count
If Not Table1.body(i, j) = "" Then
Set foundRng = Table2Groups.Find(Table1Heading(,j), LookIn:=xlValues)
outRng.Offset(k - 1).Resize(foundRng.Rows.Count).Value = foundRng.Offset(, 1).Value
k = k + foundRng.Rows.Count
Else
End If
Next j
Next i
This, obviously, only returns one value, as range.find only returns the range of the first found cell. This would be my resulting table:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD="align: center"]Fruit Types[/TD]
[TD="align: center"]Fruit Variety[/TD]
[TD="align: center"]Location Group[/TD]
[TD="align: center"]Location[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Apple[/TD]
[TD]Gala[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Apple[/TD]
[TD]Fuji[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Orange[/TD]
[TD]Tangerine[/TD]
[TD]Stores[/TD]
[TD]Store 100[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Ecommerce[/TD]
[TD]Canada Website[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Orange[/TD]
[TD]Mandarin[/TD]
[TD]Corporate[/TD]
[TD]Head Office[/TD]
[/TR]
</tbody>[/TABLE]
What am I missing? It would be great if I could use a range.resize(foundrange.size) = foundrange.offset(1) kind of formula here, but maybe I can only do this with multiple loops?
Thanks in advance!
Last edited: