I am trying to do the following in Excel:
I have a sheet with some data (400k
rows, which is why I used long for the variables instead of integers) and I want to check Column R (which contains ID's) and need to check then against Columns S and T. If R is the same and S and T is different, the code should copy the entire row and paste it in another sheet. The code runs and pastes something but not the correct rows. Thanks in advance, any help would be highly appreciated.
Sample Data
R S T
1234 Kevin Smith
2345 John Miller
1234 Carl Jones
1234 Kevin Smith
4567 Mike Redwood
2058 William Wales
Code
Sub mySub1()
Set wb = ThisWorkbook
Set tbl = wb.Sheets("sheet1")
Dim lrow As Long
Dim i As Long
Dim x As Long
Dim y As Long
Dim cell As Range
i = 1
x = 0
y = 1
Sheets("sheet1").Activate
lrow = tbl.Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In Range("R2:R" & lrow)
If cell.Offset(x, 0).Value = cell.Offset(i, 0).Value And _
cell.Offset(0, 1) <> cell.Offset(i, 1).Value And _
cell.Offset(0, 2).Value <> cell.Offset(i, 2).Value Then
ActiveSheet.Range(Cells(i + 1, 1), Cells(i + 1, 26)).Select
Selection.Copy
Sheets("sheet2").Select
ActiveSheet.Cells(y, 1).PasteSpecial
y = y + 1
End If
Sheets("sheet1").Activate
i = i + 1
x = x + 1
Next
End Sub