Generate rows in new table based on tables

glippy

New Member
Joined
Jun 18, 2021
Messages
1
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi. I am trying to generate a table on a separate sheet, based on values in three other tables. Allow me to explain. My workbook now contains three tables:

1. A table with resources (resourceid, resourcename, ...)
2. A table with projects (projectid, projectname, ...)
3. A table with weeks (weekid, year, week)

I am trying to generate a table, combining resourceid, projectid, and weekid, so that each row is a unique combination of these three variables. I have some formulas that I will add to a fourth column, and so on.

So if I would have 3 resources (1-3), 2 projects (1-2), and 4 weeks (1-4), the number of rows that need to be generated is 3 x 2 x 4 = 24. The actual number of resources, projects, and weeks is quite a bit larger of course. But for the same of explaining what I would need, the result would look kind of like this:

resourceid, projectid, weekid, columnx, columny
1, 1, 1, <column with formula>, <etc.>
1, 1, 2, <column with formula>, <etc.>
1, 1, 3, <column with formula>, <etc.>
1, 1, 4, <column with formula>, <etc.>
1, 2, 1, <column with formula>, <etc.>
1, 2, 2, <column with formula>, <etc.>
1, 2, 3, <column with formula>, <etc.>
1, 2, 4, <column with formula>, <etc.>
2, 1, 1, <column with formula>, <etc.>
2, 1, 2, <column with formula>, <etc.>
2, 1, 3, <column with formula>, <etc.>
2, 1, 4, <column with formula>, <etc.>
2, 2, 1, <column with formula>, <etc.>
2, 2, 2, <column with formula>, <etc.>
2, 2, 3, <column with formula>, <etc.>
2, 2, 4, <column with formula>, <etc.>
3, 1, 1, <column with formula>, <etc.>
3, 1, 2, <column with formula>, <etc.>
3, 1, 3, <column with formula>, <etc.>
3, 1, 4, <column with formula>, <etc.>
3, 2, 1, <column with formula>, <etc.>
3, 2, 2, <column with formula>, <etc.>
3, 2, 3, <column with formula>, <etc.>
3, 2, 4, <column with formula>, <etc.>

Is there a way of automatically generating these rows? Especially as resources and projects are added to the sheet, it would help a lot so that I don't need to manually add them.

Thanks for your help!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi, @glippy. Welcome to the Forum.
Try this:
I assumed the three tables name are Table1,Table2 & Table3

VBA Code:
Sub a1174196a()
'https://www.mrexcel.com/board/threads/generate-rows-in-new-table-based-on-tables.1174196/
Dim tbl1 As ListObject
Dim tbl2 As ListObject
Dim tbl3 As ListObject
Dim nTbl As ListObject
Dim n As Long, i As Long
Dim a, b, c

Application.ScreenUpdating = False
With ActiveSheet
    Set tbl1 = .ListObjects("Table1")
    Set tbl2 = .ListObjects("Table2")
    Set tbl3 = .ListObjects("Table3")
End With



n = tbl1.DataBodyRange.Rows.Count * tbl2.DataBodyRange.Rows.Count * tbl3.DataBodyRange.Rows.Count
    
    'change "Sheet2" and "A2" to suit
    Sheets("Sheet2").ListObjects.Add(xlSrcRange, Sheets("Sheet2").Range("A2").Resize(n + 1, 5), , xlYes).Name = "newTable"
    Set nTbl = Sheets("Sheet2").ListObjects("newTable")
    With nTbl
        .HeaderRowRange(1) = "resourceid"
        .HeaderRowRange(2) = "projectid"
        .HeaderRowRange(3) = "weekid"
    End With

For Each a In tbl1.DataBodyRange.Columns(1).Value
    For Each b In tbl2.DataBodyRange.Columns(1).Value
        For Each c In tbl3.DataBodyRange.Columns(1).Value
            i = i + 1
            With nTbl.DataBodyRange
                .Cells(i, 1) = a
                .Cells(i, 2) = b
                .Cells(i, 3) = c
            End With
        Next
    Next
Next

Application.ScreenUpdating = True
End Sub

Example:
glippy - generate-rows-in-new-table-based-on-tables.1174196.xlsm
ABCD
1
2resourceidColumn2
3Zachary
4Rodney
5Stephen
6
7
8projectidColumn2
9Q
10H
11
12
13weekidColumn2
14W01
15W02
16W03
17W04
18
Sheet1


Result:
glippy - generate-rows-in-new-table-based-on-tables.1174196.xlsm
ABCDEF
1
2resourceidprojectidweekidColumn4Column5
3ZacharyQW01
4ZacharyQW02
5ZacharyQW03
6ZacharyQW04
7ZacharyHW01
8ZacharyHW02
9ZacharyHW03
10ZacharyHW04
11RodneyQW01
12RodneyQW02
13RodneyQW03
14RodneyQW04
15RodneyHW01
16RodneyHW02
17RodneyHW03
18RodneyHW04
19StephenQW01
20StephenQW02
21StephenQW03
22StephenQW04
23StephenHW01
24StephenHW02
25StephenHW03
26StephenHW04
27
Sheet2
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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