Tuesday, 1 October 2013

Copy Excel Row if Cell is equal to a value

Copy Excel Row if Cell is equal to a value

I'm trying to copy and paste into a different workbook and spread that
data over different sheets inside of the new workbook. I've got my VBA
working, but it only works about 25% of the time. I continually get an
error on "Run-time error '1004': Select method of Range class failed".
Here is the script:
Sub CopyData()
Dim i As Range
For Each i In Range("A1:A1000")
Windows("data_1.xls").Activate
Sheets("data_1").Activate
If i.Value = 502 Then
i.Select
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Windows("DataOne.xls").Activate
Sheets("502").Range("A39").End(xlUp).Offset(1, 0).PasteSpecial
End If
If i.Value = 503 Then
........
End If
Next i
End Sub
The failure happens on i.Select every time. Do I need to bring Next i up
to the end of every End If?

No comments:

Post a Comment