Copy row from source Sheet 1 to Sheet 2 and paste below matching cell values

B4andafter

New Member
Joined
Aug 9, 2017
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hello, I am having a difficult time figuring out a way to copy rows based on cell value in column E from source worksheet Sheet 1 to Sheet 2 Master file. I would like to paste below corresponding matching cells value in column E and leave empty space between different amounts. See snippts below. Also, it would be great if you can help me list the ones that were not found. Thank you all in advance.

Sheet1 (Source)
1655073112008.png


Sheet2 (Master File)

1655073519460.png

RESULTS:

1655073587818.png


Below is the code I am using but is not working properly.

VBA Code:
Sub transfer()
Dim i As Long, j As Long, lastrow2 As Long
Dim lastrow1 As Long
Dim FinalRow As Long

Dim myNum As Integer
Dim LastRowPFW As Integer

Sheets(1).Select
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To FinalRow
myNum = Sheets(1).Cells(i, 5).Value
Sheets(2).Activate
lastrow2 = Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
For j = 2 To lastrow2
If Sheets(2).Cells(j, 5).Value = myNum Then
Sheets(1).Activate
Sheets(1).Range(Cells(i, 1), Cells(i, 5)).Copy
Sheets(2).Activate
LastRowPFW = Range("A:E").Find(what:=myNum, after:=Range("A" & i & ":E" & i), searchdirection:=xlPrevious).Row
Sheets(2).Range(Cells(j, 1), Cells(j, 5)).Select.Offset(1).Row
ActiveSheet.Paste
End If
Next j
Application.CutCopyMode = False
Next i
Sheets(1).Activate
Sheets(1).Cell(“E2”).Select
End Sub

Warm Regards,

Bfandafter from Houston, TX
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Not need to use Activate and Select. Avoid if possible. It will slow down execution. You can declare worksheet as parameter and directly call from there.
Here is my alternative
VBA Code:
Sub SortByAmount()

Dim eRow As Long
Dim cell As Range, rngSource As Range, rngMaster As Range, rngFound As Range
Dim wsSource As Worksheet, wsMaster As Worksheet

Set wsSource = ActiveWorkbook.Sheets("Source")
Set wsMaster = ActiveWorkbook.Sheets("Master")

Set rngSource = wsSource.Range("E2", wsSource.Cells(Rows.Count, "E").End(xlUp))
Set rngMaster = wsMaster.Range("E2", wsMaster.Cells(Rows.Count, "E").End(xlUp))

Application.ScreenUpdating = False

For Each cell In rngSource
    eRow = wsMaster.Cells(Rows.Count, "E").End(xlUp).Offset(1).Row
    Set rngMaster = wsMaster.Range("E2", "E" & eRow)
    Set rngFound = rngMaster.Find(cell, LookAt:=xlWhole, searchDirection:=xlPrevious)
    If Not rngFound Is Nothing Then
        If Not IsEmpty(wsMaster.Range("A" & rngFound.Row).Offset(2)) Then
            wsMaster.Range("A" & rngFound.Row).Offset(1).EntireRow.Insert
            wsSource.Range("A" & cell.Row, "E" & cell.Row).Copy wsMaster.Range("A" & rngFound.Row).Offset(1)
        Else
            wsSource.Range("A" & cell.Row, "E" & cell.Row).Copy wsMaster.Range("A" & rngFound.Row)
        End If
    Else
        If Not eRow = 2 Then
            wsMaster.Range("A" & eRow).EntireRow.Insert
            wsSource.Range("A" & cell.Row, "E" & cell.Row).Copy wsMaster.Range("A" & eRow).Offset(1)
        Else
            wsSource.Range("A" & cell.Row, "E" & cell.Row).Copy wsMaster.Range("A" & eRow)
        End If
    End If
Next

Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Fantastic sir, your alternative is much better and provide correct results. Thank you so much responding so quickly. You guys are also our heroes, you rock.

Many Blessings,
Pedro
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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