View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Per Jessen[_2_] Per Jessen[_2_] is offline
external usenet poster
 
Posts: 703
Default copy and paste a row

Hi

You have started a loop with the For i=... statement. It needs a
"Next" statement to continue the loop.
But I'm not sure the code is working as desired. If you need further
help describe in words what you desire.

Sub copy_rows()
Dim LastRow As Long, i As Long
Dim wksToSearch As Worksheet
Dim rngToSearch As Range
Dim rngFound As Range

Set wksToSearch = Sheets("Sheet1")
Set rngToSearch = wksToSearch.Columns("C")
LastRow = wksToSearch.Cells(Rows.Count, 3).End(xlUp).Row

For i = LastRow To 1 Step -1
Set rngFound = rngToSearch.Find(What:=ActiveCell, LookIn:=xlValue)
If rngFound Is Nothing Then
MsgBox "No data found"
Else
wksToSearch.Select
rngFound.EntireRow.Copy Sheets("Sheet2").Range("A3")
End If
Next
End Sub

Regards,
Per


On 22 Dec., 17:56, daisy2008
wrote:
I have played with this code but I can not make it work. *Can someone please
help me fix this? *My company has the help files locked out. *comes back as
Compile error: For without Next.

Sub copy_rows()
* * Dim lastrow As Long, i As Long
* * Dim wksToSearch As Worksheet
* * Dim rngToSearch As Range
* * Dim rngFound As Range

* * lastrow = Sheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row
* * For i = lastrow To 1 Step -1

* * Set wksToSearch = Sheets("Sheet1")
* * Set rngToSearch = wksToSearch.Columns("C")
* * Set rngFound = rngToSearch.Find(What:=ActiveCell, LookIn:=xlValue)

If rngFound Is Nothing Then
* * MsgBox "No data found"

Else
* * wksToSearch.Select
* * rngFound.EntireRow.Copy Sheets("Sheet2").Range("A3")
* * End If
End Sub