VBA to copy rows within the same sheet

shimon.amar

Board Regular
Joined
Nov 20, 2012
Messages
93
Dear fellows,

My boss gave me an assignment that I can't find a way to do it by macro.

assume I have database with lines -

NameCode NumberSub-codeDateAmount
John123412.12.12100
Jack567812.12.13300
Joe987611.12.12430

<tbody>
</tbody>

What I basically need to do is to duplicate each row 3 times and put the duplicated rows beneath the original rows.
For column "Sub-code" need to put numbers for each copied line (1, 2, 3).
For column "Amount" need to divide by 3 for each copied line.

Basically the data after macro should be like

NameCode NumberSub-codeDateAmount
John123412.12.12100
John1234112.12.1233.33
John1234212.12.1233.33
John1234312.12.1233.33
Jack567812.12.13300
Jack5678112.12.13100
Jack5678212.12.13100
Jack5678312.12.13100

<tbody>
</tbody>

****** id="cke_pastebin" style="position: absolute; top: 382px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
1234

<tbody>
</tbody>
</body>Thank in advance, very much appreciated
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Sub CopyRows3x()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i + 1).Resize(3).Insert
      Rows(i).Resize(4).FillDown
      Range("C" & i + 1).Resize(3).Value = Application.Transpose(Array(1, 2, 3))
      Range("E" & i + 1).Resize(3).Value = Range("E" & i).Value / 3
   Next i
End Sub
 
Upvote 0
How about
Code:
Sub CopyRows3x()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
      Rows(i + 1).Resize(3).Insert
      Rows(i).Resize(4).FillDown
      Range("C" & i + 1).Resize(3).Value = Application.Transpose(Array(1, 2, 3))
      Range("E" & i + 1).Resize(3).Value = Range("E" & i).Value / 3
   Next i
End Sub

Works great!! thank you very much!!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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