View Full Version : Macro assistance needed to insert and copy rows


AtLarge
01-16-2009, 08:56 AM
I tried to create a macro that does this:

Start in A1.
Go to row 2 and insert a blank row.
Copy Row 1 to the blank row 2.

Repeat this all the way down the column and stop when it hits an empty cell in column A.

It keeps repeating row 1. What am I doing wrong:confused:

Sub M_In_Copy_Row()
'
Dim Lastrow As Long, r As Long
Lastrow = Range("A65536").End(xlUp).Row
For r = Lastrow To 2 Step -1

ActiveCell.Offset(1, 0).EntireRow.Insert
ActiveCell.EntireRow.Copy ActiveCell.Offset(1, 0).EntireRow

Next r
End Sub

Brianwarnock
01-16-2009, 10:40 AM
You keep copying the row of the activecell which I guess is where your cursor is and it just happens to be in A1?

But I have no idea what you are attempting, the code does not attempt what the script suggested, and if it worked as you are hinting you would duplicate every row, is that what you want?

Brian

AtLarge
01-16-2009, 10:57 AM
Oh boy was I cornfuzed. I diddled and this works.

Sub AddRows()
Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 1 Step -1
Rows(i + 1).Insert
Rows(i + 1).FillDown
Next
End Sub

Thanx !

Brianwarnock
01-16-2009, 11:07 AM
So you are just duplicating ever row, wierd?

Brian

AtLarge
01-18-2009, 12:55 PM
So you are just duplicating ever row, wierd?

Brian

Yes. I downloaded 88,000 records from SAP. Many of which need to be corrected. Once I chopped out all the good information all I had left was the bad data. Most of which had "OR" strings in it. By inserting a row and copying the data for each original line I only had to maintainence one field rather than copying all of it and then fixing it besides. In otherwords split the OR statement from one line of "Garbage OR Moregarbage" to two lines. One of Garbage and one of Moregarbage.

I'm sure there are more eloquent ways of doing it but this is the best my feeble mind could come up with. I am way rusty and feel like I'm relearning everything all over again. Bouncing back and forth from Access to Excel is twisting my mind. I do appreciate your help though. ;)

Brianwarnock
01-19-2009, 05:50 AM
Thanks for the feedback, its probably a nice simple approach which IMHO is always worth while.

Bouncing back and forth from Access to Excel is twisting my mind
Yeah, I used to find myself wondering why IIF s wouldn't work in EXCEL. :D

Brian