I want to use following VBA code to link data from two sheets which are within same workbook. Each entry in both sheets have a unique identifier. I am hoping to use that identifier and copy whole row from sheet2 and past it on the right hand side of sheet1's last column.
How to fix this code?
Sub link_data()
Dim i, lastrow
im i2, lastrow2
Dim A As Double
Dim D As Double
lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
For i2 = 2 To lastrow2
Set D = Sheet1.Cells(i, "AW")
Set A = Sheet2.Cells(i2, "AI")
If Sheet1.Cells(D).Value = Sheet2.Cells(A) Then
Sheet2.Cells(A).EntireRow.Copy Destination:=Sheet1.Cells(i, "AX").end(xlRight).Offset(1)
End Sub
For
withNext
(you have 2For
loops), and also close yourIf
withEnd If
. – Shai Rado