Copy Paste from specific row to specific row in another sheet

dugden

New Member
Joined
Mar 2, 2016
Messages
1
Sheet A
ABC
1NamePolicyAmount
2Paul23450
3Paul234100
4Sharon36520
5sharon36540
6shane976100
7andrea57330
8andrea57330
9andrea57350

<tbody>
</tbody>

Sheet B
ABC
NamePolicyAmount
1Andrea573100
2Gary49770
4Shane976100
5Paul234140
6Sharon36590

<tbody>
</tbody>


Sheet C
ABC
NamePolicyAmount
1Andrea573100
2Andrea57330
3Andrea57330
4Andrea57350
5Gary49770
6Shane976100
7Paul234140
8Paul23450
9Paul234100
10Sharon36590
11Sharon36520
11Sharon36540

<tbody>
</tbody>


I need to produce a report as detailed in Sheet C being a concatenation of SheetsA & B as outlined.
All similar policy found in Sheet A are copied and inserted at the cell below it's corresponding policy.
So in the instance where Paul with policy number 234 is found in SheetB his matching policy is copied (entire row) form Sheet A into sheet B

Note. I have created a copy of sheet B which is used to loop over the policy range. This sheet will be the source and used to move the rows between sheets.


Here is the code thus far

Sub Macro2()
Dim i As Long, j As Long, RefLastRow As Long, MarLastRow As Long, WSLastRow As Long
Dim refPolicy As String, myWkShtAddr As String
Dim ws As Worksheet, mar As Worksheet, ref As Worksheet
Dim wsRange As Range
Dim k As Integer



Set ws = Sheets("Worksheet")
Set mar = Sheets("Marathon")
Set ref = Sheets("WS_Data")


RefLastRow = ref.Range("H" & Rows.Count).End(xlUp).Row
WSLastRow = ws.Range("H" & Rows.Count).End(xlUp).Row


For i = 2 To RefLastRow
refPolicy = ref.Cells(i, "H").Value
mar.Activate
MarLastRow = mar.Range("N" & Rows.Count).End(xlUp).Row
For j = 2 To MarLastRow
If mar.Cells(j, "N").Value = refPolicy Then
On Error Resume Next
With ws
Set wsRange = ws.Range(“H2:H” & WSLastRow).Find(What:=refPolicy, Lookin:=xlValues) '\\This gives error,not sure why.




If Not wsRange Is Nothing Then
k = 0
firstAddress = wsRange.Address
Do
mar.Select
mar.Range(Cells(j, "A")).Copy
ws.Select
NextRow = ws.Range.Address.Row + 1 '\\ Not sure how to do the insert here
Cells(NextRow, 1).Select
ActiveSheet.Paste


mar.Range(Cells(j)).EntireRow.Copy Destination:=ws.Range(wsRange).Offset(j, k)




Set wsRange = .FindNext(wsRange)
Loop While Not wsRange Is Nothing And wsRange.Address <> firstAddress

End If
End With

End If
Next j
Application.CutCopyMode = False
Next i
ref.Activate
ref.Range("A1").Select


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Why not copy all the rows in sheet ("A") and paste them into sheet ("B") and then sort sheet "B" by policy number?
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,957
Members
449,200
Latest member
indiansth

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