Macro to copy rows multiple times

JHolden47

New Member
Joined
Jun 12, 2019
Messages
6
I have seen a number of similar threads on this subject, however I have a variation.

I have a sheet with 211 rows, each row needs to be copied and inserted multiple times. The number of times it needs to be copied is in a cell within each row (Col G). So The first row has the number 14 in Col G so this needs to be copied 14 times below, this then needs to loop for all the 211 rows.

Any ideas?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi & welcome to MrExcel.
How about
Code:
Sub JHolden47()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i).Copy
      Rows(i).Resize(Range("G" & i)).Insert
   Next i
End Sub
 
Upvote 0
That worked perfectly, thanks ver much for your help. The next step is I want to insert a row and add a unique ID which is the ID already in Col H with AA, AB, AC etc, then repeated for the next group. so

WTL-006-033-4-AA
WTL-006-033-4-AB
WTL-006-033-4-AC
SFC-006-113-4-AA
SFC-006-113-4-AB
 
Upvote 0
How about
Code:
Sub JHolden47()
   Dim i As Long, j As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i).Copy
      Rows(i).Resize(Range("G" & i)).Insert
      For j = 0 To Range("G" & i).Value
         Range("H" & i + j).Value = Range("H" & i + j) & "-A" & Chr(j + 65)
      Next j
   Next i
End Sub
This assumes you will never have more than 26 duplicate rows
 
Last edited:
Upvote 0
There are several occurrences where I exceed 26 duplicate rows, in one case there are 685, so it needs to repeat AA-AZ, BA-BZ, CA-CZ, DA-DZ etc
 
Upvote 0
Ok, how about
Code:
Sub JHolden47()
   Dim i As Long, j As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i).Copy
      Rows(i).Resize(Range("G" & i)).Insert
      For j = 0 To Range("G" & i).Value
         Range("H" & i + j).Value = Range("H" & i + j) & "-" & Split(Cells(i, j + 27).Address(1, 0), "$")(0)
      Next j
   Next i
End Sub
 
Upvote 0
Ok, how about
Code:
Sub JHolden47()
   Dim i As Long, j As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i).Copy
      Rows(i).Resize(Range("G" & i)).Insert
      For j = 0 To Range("G" & i).Value
         Range("H" & i + j).Value = Range("H" & i + j) & "-" & Split(Cells(i, j + 27).Address(1, 0), "$")(0)
      Next j
   Next i
End Sub

I tried running that, it deleted the first 14 rows and crashed excel
 
Upvote 0
As that code doesn't delete anything, it sounds like you ahve something else going on.
Do you have any event macros running on that sheet?
 
Upvote 0
Did you run the code from post#6 after running the code from post#2?
If so don't as the one code does both
delete the code you have got & try this, which should be a bit quicker
Code:
Sub JHolden47()
   Dim i As Long, j As Long
   
   Application.ScreenUpdating = False
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i).Copy
      Rows(i).Resize(Range("G" & i)).Insert
      For j = 0 To Range("G" & i).Value
         Range("H" & i + j).Value = Range("H" & i + j) & "-" & Split(Cells(i, j + 27).Address(1, 0), "$")(0)
      Next j
   Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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