Duplicate rows and fill in new columns based on master list

lata_m

New Member
Joined
Nov 12, 2019
Messages
3
Hello all,

This is my first time posting and I am hoping you can help me out with this problem in Excel.

I have a Final sheet which has locations repeated a fixed n number of times (in this case, n=2) and has its own property columns as shown below:


[TABLE="width: 500"]
<tbody>[TR]
[TD]Location[/TD]
[TD]Property3[/TD]
[TD]Property4[/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Z[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Z[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I would like to duplicate rows for individual locations and its accompanying property 3 and 4 columns as well as add two new property columns based on Master List as shown below:


[TABLE="width: 500"]
<tbody>[TR]
[TD]Location[/TD]
[TD]Property1[/TD]
[TD]Property2[/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD]a[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD]a[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD]a[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD]b[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Z[/TD]
[TD]a[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD]a[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD]b[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]


Modified Final sheet should look like:


[TABLE="width: 500"]
<tbody>[TR]
[TD]Location[/TD]
[TD]Property1[/TD]
[TD]Property2[/TD]
[TD]Property3[/TD]
[TD]Property4[/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD]a[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD]a[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD]a[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]X[/TD]
[TD]a[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD]a[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD]a[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD]b[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Y[/TD]
[TD]b[/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Z[/TD]
[TD]a[/TD]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Z[/TD]
[TD]a[/TD]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD]a[/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD]a[/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD]b[/TD]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]XX[/TD]
[TD]b[/TD]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Locations X, Y and XX have been duplicated twice as it has had to capture the unique values in property columns in 1 and 2. Z only once as it has unique Property 1 and 2 values.

Thanks a lot!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
with Power Query

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#5B9BD5]Location[/td][td=bgcolor:#5B9BD5]Property1[/td][td=bgcolor:#5B9BD5]Property2[/td][td][/td][td=bgcolor:#70AD47]Location[/td][td=bgcolor:#70AD47]Property1[/td][td=bgcolor:#70AD47]Property2[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]X[/td][td=bgcolor:#DDEBF7]a[/td][td=bgcolor:#DDEBF7]
1​
[/td][td][/td][td=bgcolor:#E2EFDA]X[/td][td=bgcolor:#E2EFDA]a[/td][td=bgcolor:#E2EFDA]
1​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]X[/td][td]a[/td][td]
2​
[/td][td][/td][td]X[/td][td]a[/td][td]
1​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Y[/td][td=bgcolor:#DDEBF7]a[/td][td=bgcolor:#DDEBF7]
3​
[/td][td][/td][td=bgcolor:#E2EFDA]X[/td][td=bgcolor:#E2EFDA]a[/td][td=bgcolor:#E2EFDA]
2​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]Y[/td][td]b[/td][td]
3​
[/td][td][/td][td]X[/td][td]a[/td][td]
2​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]Z[/td][td=bgcolor:#DDEBF7]a[/td][td=bgcolor:#DDEBF7]
4​
[/td][td][/td][td=bgcolor:#E2EFDA]Y[/td][td=bgcolor:#E2EFDA]a[/td][td=bgcolor:#E2EFDA]
3​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]XX[/td][td]a[/td][td]
5​
[/td][td][/td][td]Y[/td][td]a[/td][td]
3​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]XX[/td][td=bgcolor:#DDEBF7]b[/td][td=bgcolor:#DDEBF7]
6​
[/td][td][/td][td=bgcolor:#E2EFDA]Y[/td][td=bgcolor:#E2EFDA]b[/td][td=bgcolor:#E2EFDA]
3​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td]Y[/td][td]b[/td][td]
3​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td=bgcolor:#E2EFDA]Z[/td][td=bgcolor:#E2EFDA]a[/td][td=bgcolor:#E2EFDA]
4​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td]Z[/td][td]a[/td][td]
4​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td=bgcolor:#E2EFDA]XX[/td][td=bgcolor:#E2EFDA]a[/td][td=bgcolor:#E2EFDA]
5​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td]XX[/td][td]a[/td][td]
5​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td=bgcolor:#E2EFDA]XX[/td][td=bgcolor:#E2EFDA]b[/td][td=bgcolor:#E2EFDA]
6​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td][/td][td][/td][td][/td][td][/td][td]XX[/td][td]b[/td][td]
6​
[/td][/tr]
[/table]


Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    List = Table.AddColumn(Source, "Custom", each {1..2}),
    Expand = Table.ExpandListColumn(List, "Custom"),
    RC = Table.RemoveColumns(Expand,{"Custom"})
in
    RC[/SIZE]

is that what you want?
 
Upvote 0
Hi @, welcome to the forum!

Try the following macro.
Change "Sheet1" (Sheet with locations), "Master" and "Sheet3" (Sheet with result), for the names of your sheets.

Code:
Sub Duplicate_rows()
  Dim a() As Variant, b() As Variant, c() As Variant, i As Long, j As Long, n As Long
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Set sh1 = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
  Set sh2 = Sheets("[COLOR=#ff0000]Master[/COLOR]")
  Set sh3 = Sheets("[COLOR=#ff0000]Sheet3[/COLOR]")
  sh3.Cells.ClearContents
  a = sh1.Range("A2", sh1.Range("C" & sh1.Range("A" & Rows.Count).End(xlUp).Row))
  b = sh2.Range("A2", sh2.Range("C" & sh2.Range("A" & Rows.Count).End(xlUp).Row))
  ReDim c(1 To (UBound(a, 1) * UBound(b, 1)), 1 To 5)
  n = 1
  For i = 1 To UBound(b, 1)
    For j = 1 To UBound(a, 1)
      If a(j, 1) = b(i, 1) Then
        c(n, 1) = b(i, 1)
        c(n, 2) = b(i, 2)
        c(n, 3) = b(i, 3)
        c(n, 4) = a(j, 2)
        c(n, 5) = a(j, 3)
        n = n + 1
      End If
    Next
  Next
  sh3.Range("A2").Resize(n, 5).Value = c()
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,525
Members
452,651
Latest member
wordsearch

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