Duplicate Rows based off list

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
I have a list where i have a specific number of rows needed for each object ID. However i cant for the life of me think of a quick way to do it. I know there is a way to do it by transforming but keep running into issues

Table below. Small example below but this goes to 7000 object IDs. The idea would be that I run some code and in column A it would have the Object ID 1 twice, then below it the Object ID2 three times then the object id 3 6 times and so on, does this make sense?

OBJECTIDRows Needed
1​
2​
2​
3​
3​
6​
4​
2​
5​
2​
6​
3​
7​
3​
8​
3​
9​
3​
10​
3​

Happy to use formulas or code whichever gets the result really, thanks in advance
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try:
VBA Code:
Sub dup_row()
Dim i As Long, j As Long, k As Long
Dim va, vb

va = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
ReDim vb(1 To 500000, 1 To 2) ''I assumed the result won't exceed 500K rows

For i = 1 To UBound(va, 1)
    For j = 1 To va(i, 2)
        k = k + 1
        vb(k, 1) = va(i, 1)
        vb(k, 2) = va(i, 2)
    Next
Next

Range("A2").Resize(k, 2) = vb

End Sub

Result:
Book1
AB
1OBJECTIDRows Needed
212
312
423
523
623
736
836
936
1036
1136
1236
1342
1442
1552
1652
1763
1863
1963
2073
2173
2273
2383
2483
2583
2693
2793
2893
2913
3013
3113
Sheet2
 
Upvote 0
Absolutely perfect that worked exactly as I was looking for, thank you so much
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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