Insert and Copy Data to new Workbook Row

  • Thread starter Thread starter Ozzie via OfficeKB.com
  • Start date Start date
O

Ozzie via OfficeKB.com

I need to insert a new row into a workbook where a character in Column "A" =
"D" and then copy the row which is two rows above into the new inserted row.

I can insert the row ok with the following code;

Sub Add_Rows()

Dim lastrow As Long, i As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 1) = "D" Then
Rows(i).Insert
End If
Next
End Sub

but what I can't do is copy the cells in 2 rows above this newly inserted row?
..

I have been searching and reading numerous threads and links but to no avail.

Can anyone assist?, i am using 2003.
 
Does this work?

Option Explicit
Sub Add_Rows()

Dim lastrow As Long, i As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 1) = "D" Then
Rows(i - 2).Copy
Rows(i + 1).Insert
End If
Next i
End Sub
 
Hi Dave, (I re-posted this post again in the Programming Forum, instead of
new users)

Cheers for your speedy response, unfortunately though the code isn't working
properly.

I think the problem is that the code appears to copy from bottom up and not
top down as is needed, apologies for not being clearer.

Regards

David


Dave said:
Does this work?

Option Explicit
Sub Add_Rows()

Dim lastrow As Long, i As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 1) = "D" Then
Rows(i - 2).Copy
Rows(i + 1).Insert
End If
Next i
End Sub
I need to insert a new row into a workbook where a character in Column "A" =
"D" and then copy the row which is two rows above into the new inserted row.
[quoted text clipped - 19 lines]
Can anyone assist?, i am using 2003.
 
It seemed to match exactly what you requested for me.





Ozzie via OfficeKB.com said:
Hi Dave, (I re-posted this post again in the Programming Forum, instead of
new users)

Cheers for your speedy response, unfortunately though the code isn't working
properly.

I think the problem is that the code appears to copy from bottom up and not
top down as is needed, apologies for not being clearer.

Regards

David

Dave said:
Does this work?

Option Explicit
Sub Add_Rows()

Dim lastrow As Long, i As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 1) = "D" Then
Rows(i - 2).Copy
Rows(i + 1).Insert
End If
Next i
End Sub
I need to insert a new row into a workbook where a character in Column "A" =
"D" and then copy the row which is two rows above into the new inserted row.
[quoted text clipped - 19 lines]
Can anyone assist?, i am using 2003.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top