Macro - copy and insert rows of data based on value in one column

lcmast401

New Member
Joined
May 16, 2019
Messages
4
I have read many posts similar to this but can't get my vba code to work. Please help.
Sample data:

A1A2A3A4A5A6A7

<tbody>
</tbody>
21609Gold $10Person11$10Raffle1
21609Gold $10Person29$90Raffle1
21609Gold $10Person36$60Raffle1





<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
Based on the value in a5 - if it is greater than 1, I want it to copy and insert the same line x-1 times. For example in the second line, the value in a5 is 9, so i would like to see that exact line 8 addition times, or 9 times in total. For person3, where the value is 6, I would like to see that line 6 times in total, etc.

Thank you for the help!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi & welcome to MrExcel.
How about
Code:
Sub lcmast401()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      If Cells(i, 5) > 1 Then
         Rows(i).Copy
         Rows(i + 1).Resize(Cells(i, 5) - 1).Insert
      End If
   Next i
   Application.CutCopyMode = False
End Sub
 
Upvote 0
Thank you!!! That worked perfectly!



Hi & welcome to MrExcel.
How about
Code:
Sub lcmast401()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      If Cells(i, 5) > 1 Then
         Rows(i).Copy
         Rows(i + 1).Resize(Cells(i, 5) - 1).Insert
      End If
   Next i
   Application.CutCopyMode = False
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
I've added some comments, that may help you to understand it.
Code:
Sub lcmast401()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1 ' loops through the data starting at the last row & working up
      If Cells(i, 5) > 1 Then 'checks to see if the value in col E for that row is greater then 1
         Rows(i).Copy ' copies the row
         Rows(i + 1).Resize(Cells(i, 5) - 1).Insert 'inserts the copied row x amount of times (determined by the value in col E)
      End If
   Next i
   Application.CutCopyMode = False
End Sub
 
Upvote 0
Thank you again!



You're welcome & thanks for the feedback.
I've added some comments, that may help you to understand it.
Code:
Sub lcmast401()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1 ' loops through the data starting at the last row & working up
      If Cells(i, 5) > 1 Then 'checks to see if the value in col E for that row is greater then 1
         Rows(i).Copy ' copies the row
         Rows(i + 1).Resize(Cells(i, 5) - 1).Insert 'inserts the copied row x amount of times (determined by the value in col E)
      End If
   Next i
   Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

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