VBA for inserting rows and copying the value above

lintel

New Member
Joined
Jul 6, 2011
Messages
3
Hi everyone.

I have a table with 2 columns A and B. Both columns contain a long list of numbers. I would like to run a makro that has two steps.

1) Insert a new row inbetween each row (my makro below does that already)
2) After inserting a new row it shall copy the value from the row above.

In other words. I have columns A and B and five rows as shown below.

A B
6 3
4 5
3 1
2 3

The makro shall then insert a new row between each line and copy the value from the cells above, so that the result will be:

A B
6 3
6 3
4 5
4 5
3 1
3 1
2 3
2 3

My makro is missing the 2nd step (copying the values from above). It currently looks like this:

Sub Insert_Blank_Rows()

'Select last row in worksheet.
Selection.End(xlDown).Select

Do Until ActiveCell.Row = 1
'Insert blank row.
ActiveCell.EntireRow.Insert shift:=xlDown
'Move up one row.
ActiveCell.Offset(-1, 0).Select

Loop

End Sub

Does anyone know how I include the 'copying from above' part?

Thank you so much,
Alex
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try

Code:
Sub Insert_Blank_Rows()
Dim LR As Long, i As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For i = LR To 1 Step -1
With Range("A" & i).EntireRow
    .Copy
    .Insert shift:=xlDown
    End With
Next i
End Sub
 
Upvote 0
try:

Code:
Sub insert()
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = lr To 2 Step -1
        With Rows(i)
            .Copy
            .Offset(1).insert xlDown
        End With
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,522
Members
452,923
Latest member
JackiG

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