How to fill a

elmacay

Board Regular
Joined
May 4, 2006
Messages
88
Hello everyone,

Edit: Using Excel 2016

I am having difficulties describing my question, so I didn't know how to search for it on the forum. Hope I can make myself clear through a picture. I guess I'm not the first one to ask this.

So, I want to create a filled table 3. This table consists of an x number of IDs, with for every ID a y number of Attributes (properties). This creates a table of in this case 3x3 unique combinations. The purpose of this table is to have it filled out with values, and after to be uploaded. The steps would be 1) fill table with IDs; 2) fill table with Attributes; 3) press button with macro --> tadaa, filled table number 3; 4) fill out values; 5) save and upload.

I'm not sure if working with tables here helps or hinders. I thought it would make it easier, because I could refer to the tables in the code.

Any help is greatly appreciated!

Cheers, Elmacay
 

Attachments

  • IDs times Attributes.png
    IDs times Attributes.png
    23 KB · Views: 4
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You may want to update your profile to show the version of Excel that you use. You want to create a table consisting of all "combinations" of items in two columns. This could be done either with formulas or VBA. Please respond back with your version and preference for constructing the table.
 
Upvote 0
My apologies I forgot to change the name of the thread after writing it. And there doesn't seem to be a way to do that.
The title should be 'How to automatically fill a table combining two other tables'.
 
Upvote 0
Thank you, KRice, good tips.
I think a macro would be most handy, since the product of IDs and Attributes can run up quickly.
 
Upvote 0
Here is one way to do it with formulas: You can hide columns H and I if you'd like...they create the combinations of indexes for pulling info from each of the other tables. As you add information to the source tables, just pull the formulas in the combination table down plenty of extra rows to accommodate the larger number of combinations.
mrexcel_20200416.xlsm
ABCDEFGHIJKL
1
2IDsAttributesCountsRowNumRow_AttIDAttributeValue
3ID1Attrib1ID311ID1Attrib1
4ID2Attrib2Attributes412ID1Attrib2
5ID3Attrib3num rows1213ID1Attrib3
6Attrib414ID1Attrib4
721ID2Attrib1
822ID2Attrib2
923ID2Attrib3
1024ID2Attrib4
1131ID3Attrib1
1232ID3Attrib2
1333ID3Attrib3
1434ID3Attrib4
15    
16    
17    
18    
19    
20    
21    
22    
23
24
25
26
27
Sheet21
Cell Formulas
RangeFormula
H3:H22H3=IF(ROWS(H$3:H3)<=cnt_ID*cnt_Att,QUOTIENT(ROWS(H$3:H3)-1,cnt_Att)+1,"")
I3:I22I3=IF(H3<>"",MOD(ROWS(H$3:H3)-1,cnt_Att)+1,"")
J3:J22J3=IFERROR(OFFSET(Table1[[#Headers],[IDs]],H3,0),"")
K3:K22K3=IFERROR(OFFSET(Table2[[#Headers],[Attributes]],I3,0),"")
F3F3=COUNTA(Table1[IDs])
F4F4=COUNTA(Table2[Attributes])
F5F5=cnt_ID*cnt_Att
Named Ranges
NameRefers ToCells
cnt_Att=Sheet21!$F$4F5, H3:I22
cnt_ID=Sheet21!$F$3F5, H3:H22
tbl_Att=Table2[[#All],[Attributes]]K3:K22
tbl_ID=Table1[[#All],[IDs]]J3:J22
 
Upvote 0
And a solution with VBA could be used to build the combination list from two source columns as shown here. Source lists in columns A & B. Combinations written into columns D & E. Adjust the VBA code references to suit.

mrexcel_20200416.xlsm
ABCDE
1
2ID1Attrib1ID1Attrib1
3ID2Attrib2ID1Attrib2
4ID3Attrib3ID1Attrib3
5ID4Attrib4ID1Attrib4
6Attrib5ID1Attrib5
7ID2Attrib1
8ID2Attrib2
9ID2Attrib3
10ID2Attrib4
11ID2Attrib5
12ID3Attrib1
13ID3Attrib2
14ID3Attrib3
15ID3Attrib4
16ID3Attrib5
17ID4Attrib1
18ID4Attrib2
19ID4Attrib3
20ID4Attrib4
21ID4Attrib5
Sheet37


VBA Code:
Option Explicit

Sub combination()

Dim wb1 As Workbook
Dim ws1 As Worksheet

' Set references
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Sheet37")

' Get last non-empty rows
Dim lrw1, lrw2 As Integer
lrw1 = ws1.Cells(1000, "A").End(xlUp).Row
lrw2 = ws1.Cells(1000, "B").End(xlUp).Row

' Create loops to cycle through two lists
Dim i, j, k As Integer
k = 2                         ' row where data begins
For i = 2 To lrw1             ' loop starts on row where data begins
For j = 2 To lrw2             ' loop starts on row where data begins
    ws1.Cells(k, "D").Value = ws1.Cells(i, "A").Value
    ws1.Cells(k, "E").Value = ws1.Cells(j, "B").Value
    k = k + 1
Next j
Next i

End Sub
 
Upvote 0
Thanks a lot, KRice. Works like a charm. I'll tinker with the code to help me understand what it does.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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