Excel Vba - How to copy and paste matched rows from one sheet to below exact matched rows in another sheet

fahadun

New Member
Joined
Jul 27, 2017
Messages
22
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.



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:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try with this

Code:
Sub copy_paste_matched_rows()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim u1 As Double
    
    Application.ScreenUpdating = False
    
    Set sh1 = Sheets("sheet1")
    Set sh2 = Sheets("sheet2")
    
    u1 = sh1.Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To u1
        Set b = sh2.Columns("A").Find(sh1.Cells(i, "A"), lookat:=xlWhole, LookIn:=xlValues)
        If Not b Is Nothing Then
            sh1.Rows(1).Copy
            sh2.Rows(b.Row + 1).Insert Shift:=xlDown
            sh1.Rows(i).Copy
            sh2.Rows(b.Row + 2).Insert Shift:=xlDown
        End If
    Next
    
    Application.ScreenUpdating = True
    
    MsgBox "End"
End Sub
 
Upvote 0
Try with this

Code:
Sub copy_paste_matched_rows()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim u1 As Double
    
    Application.ScreenUpdating = False
    
    Set sh1 = Sheets("sheet1")
    Set sh2 = Sheets("sheet2")
    
    u1 = sh1.Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To u1
        Set b = sh2.Columns("A").Find(sh1.Cells(i, "A"), lookat:=xlWhole, LookIn:=xlValues)
        If Not b Is Nothing Then
            sh1.Rows(1).Copy
            sh2.Rows(b.Row + 1).Insert Shift:=xlDown
            sh1.Rows(i).Copy
            sh2.Rows(b.Row + 2).Insert Shift:=xlDown
        End If
    Next
    
    Application.ScreenUpdating = True
    
    MsgBox "End"
End Sub

Thank you. That Helped a lot.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top