How to duplicate multiple rows based on cell values

JWPE

New Member
Joined
Feb 2, 2022
Messages
4
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
Hello Everybody,

I currently have a macro below that has worked great.
I had to add in an extra column recently, so now the quantity is sourced from column H instead of column G.
I tried to make the adjustments to the formula but I was unsuccessful.
Columns A-G now need to be duplicated based off column H
Columns I and J are used in conjunction with columns A & G, but do not need to be duplicated.
Having the data populate in M
Can someone guide me through this?
Thanks in advance

Sub DuplicateMultipleRows_2()
Dim a As Variant, b As Variant
Dim i As Long, j As Long, k As Long, x As Long, lr As Long, m As Long

lr = Range("G" & Rows.Count).End(3).Row
a = Range("A2:G" & lr).Value
x = WorksheetFunction.Sum(Range("G2:G" & lr))
ReDim b(1 To x, 1 To UBound(a, 2))

For i = 1 To UBound(a, 1)
For j = 1 To a(i, 7)
k = k + 1
For m = 1 To UBound(a, 2)
b(k, m) = a(i, m)
Next
Next
Next

Range("M2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 

Attachments

  • Mr Excel.png
    Mr Excel.png
    14 KB · Views: 7

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi JWPE,

Try this:

VBA Code:
Option Explicit
Sub DuplicateMultipleRows_3()

    'https://www.mrexcel.com/board/threads/how-to-duplicate-multiple-rows-based-on-cell-values.1218604

    Dim a As Variant, b As Variant
    Dim i As Long, j As Long, k As Long, x As Long, lr As Long, m As Long
    
    Application.ScreenUpdating = False
    
    lr = Range("H" & Rows.Count).End(3).Row
    a = Range("A2:H" & lr).Value
    x = WorksheetFunction.Sum(Range("H2:H" & lr))
    ReDim b(1 To x, 1 To UBound(a, 2))
    
    For i = 1 To UBound(a, 1)
        For j = 1 To a(i, UBound(a, 2))
            k = k + 1
            For m = 1 To UBound(a, 2)
                b(k, m) = a(i, m)
            Next m
        Next j
    Next i
    
    Range("M2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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