PDA

View Full Version : Copying with condition



11@e
08-15-2019, 02:45 PM
I want to copy the rows that contain the word "Other" in column A from sheet D and paste them into sheet SORT - it is not giving an error and not doing anything. What am I doing wrong?



Sub Command()


a = Worksheets("D").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To a
If Worksheets("D").Cells(i, 1).Value = "Other" Then

Worksheets("D").Rows(i).Copy
Worksheets("SORT").Rows(i).Activate
b = Worksheets("SORT").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("SORT").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("D").Activate

End If
Next

Application.CutCopyMode = False
ThisWorkbook.Worksheets("D").Cells(1, 1).Select

End Sub

p45cal
08-15-2019, 05:22 PM
try:
Sub Command()
With Worksheets("D")
For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
If .Cells(i, 1).Value = "Other" Then .Rows(i).Copy Worksheets("SORT").Cells(Rows.Count, 1).End(xlUp).Offset(1)
Next
Application.Goto .Cells(1, 1)
End With
End Sub