I'm relatively new to VBA coding and would appreciate any help and guidance.
I'm trying to compare values in column “A” in on 2 different worksheets - source and destination - and if values match - overwrite the entire row in the destination, if a value doesn't exist then I would like the macro to add a new row after last row on the destination sheet with the row of data from the source sheet, corresponding to the cell that was not found. The code works perfectly fine for overwriting the row, but I'm really stuck with adding a new row. I tried putting an Else inside the loop, and it's adding a new row, but when I run macro again it add that same row again after last row instead of overwriting. What am I doing wrong?
Code:
Dim source, desination As Worksheet
Dim lastrow, lastrow2 As Long
Set source = Sheets("Sheet2")
Set destination = Sheets("Sheet1")
lastrow = source.Cells(Rows.Count, 1).End(xlUp).Row
lastrow2 = destination.Cells(Rows.Count, 1).End(xlUp).Row
For j = 2 To lastrow
For i = 2 To lastrow2
If Trim(source.Cells(j, 1).Value2) = vbNullString Then Exit For
If destination.Cells(i, 1).Value = source.Cells(j, 1).Value Then
destination.Range("A"& i, "O"& i).Value = source.Range("A"& j, "O"& j).Value
Else
destination.Range("A"& lastrow2 + 1, "O"& lastrow2 + 1).Value = source.Range("A"& j,"O"& j).Value
End If
Next
Next