Lindsay0385
New Member
- Joined
- Dec 21, 2016
- Messages
- 30
Hello -
I found a code online that I'm trying to edit to fit my situation, but I keep on getting errors when I run it.
Here's what I want to do: Column A is empty except for cells with the value "x". I want to copy the rows that contain "x" in Column A and insert the rows directly below the last row with the value "x". There are a total of 11 cells that contain "x" in Column A and they are continuous.
I originally had a macro that worked with copying and pasting specific rows, but it no longer worked if rows were inserted above the rows I wanted to copy, so I was hoping marking the rows with "x" then copying them could be a work around.
Advise would be appreciated! Thanks, Lindsay
I found a code online that I'm trying to edit to fit my situation, but I keep on getting errors when I run it.
Here's what I want to do: Column A is empty except for cells with the value "x". I want to copy the rows that contain "x" in Column A and insert the rows directly below the last row with the value "x". There are a total of 11 cells that contain "x" in Column A and they are continuous.
I originally had a macro that worked with copying and pasting specific rows, but it no longer worked if rows were inserted above the rows I wanted to copy, so I was hoping marking the rows with "x" then copying them could be a work around.
Advise would be appreciated! Thanks, Lindsay
Code:
Sub copy_rows()
'Determine last Row with data in Column A
lastRw = Cells(Rows.Count, "A").End(xlUp).Row
'Loop through rows in reverse order
For rw = lastRw To 4 Step -1
'If Column A = "x", insert Rows
If Cells(rw, "A") = "x" Then
For newRw = 1 To Cells(rw, "A")
Cells(rw, "A").EntireRow.Copy
Cells(rw, "A").EntireRow.Insert shift:=xlDown
Next
End If
Next
End Sub