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:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,889
Office Version
2007
Platform
Windows
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
 

fahadun

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

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,889
Office Version
2007
Platform
Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,082,305
Messages
5,364,401
Members
400,800
Latest member
germanpbv

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top