I am fairly new in excel vba scenario. What I am trying to accomplish here in this macro is,I have two sheet, two column, sheet1 Column A, sheet2 Column A, both have possible matches in column A. I am trying to find all the matches between two sheets and copy matched entire rows from sheet1 to exactly below matched rows in sheet two with the header of sheet1.
sheet1
Data-----------name
012-----------AAA
022-----------BBB
033-----------CCC
Sheet2
id-----------address
012-----------NYC
021-----------Philly
033-----------CT
Result
id-----------address
012-----------NYC
Data-----------name
012-----------AAA
021-----------Philly
033-----------CT
Data-----------name
033-----------CCC
The code I have so far only copying the first row 3 times, no idea how to fix it.
sheet1
Data-----------name
012-----------AAA
022-----------BBB
033-----------CCC
Sheet2
id-----------address
012-----------NYC
021-----------Philly
033-----------CT
Result
id-----------address
012-----------NYC
Data-----------name
012-----------AAA
021-----------Philly
033-----------CT
Data-----------name
033-----------CCC
The code I have so far only copying the first row 3 times, no idea how to fix it.
Code:
Sub oneMacro()
Dim lastrowone As Integer, lastrowtwo As Integer
lastrowone = Sheets("Sheet1").Cells(Rows.Count,1).End(xlUp).Row
lastrowtwo = Sheets("Sheet2").Cells(Rows.Count,1).End(xlUp).Row
For i =1 To lastrowone
For j =1 To lastrowtwo
If Sheets("Sheet1").Cells(i,1).Value = Sheets("Sheet2").Cells(j,1).Value Then
Sheets("Sheet1").Cells(i,1).EntireRow.Copy
Sheets("Sheet2").Cells(j,1).Offset(1).Insert Shift:=xlDown
EndIf
Next j
Next I
End Sub
Last edited: