copy & paste using looping

jane5

New Member
Joined
Oct 26, 2016
Messages
1
Code:
Sub dataloop()
Lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To Lastrow
If Cells(i, 3) = "Pass" Then
ActiveCell.Offset(-4).Copy
ActiveCell.Offset(2, -2).PasteSpecial
End If


Next i


End Sub

I am trying to use looping to copy and paste the value where C= Pass, then go up 4 rows and copy and paste into 2 rows down and 2 rows left, from cells containing Pass. However, It's not woking as I what need it to do. It gives me errors, and I do not know how to fix this...

Is there any solution for this?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try:

Sub dataloop()
Lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To Lastrow
If Cells(i, 3) = "Pass" Then
Cells(i, 3).Offset(-4, 0).Copy
Cells(i, 3).Offset(2, -2).PasteSpecial
End If


Next i


End Sub
More elegantly, use a with:
Code:
Sub dataloop()
Dim Lastrow as Long
Lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To Lastrow
   With Cells(i,3)
      If .Value = "Pass" Then
         .Offset(-4,0).Copy
         .Offset(2, -2).PasteSpecial
      End If
   End With
Next i


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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