Compare Cells then move them

whitefrodude

New Member
Joined
Mar 4, 2014
Messages
2
I have 2 sheets with receipt numbers on them. What I want to do is run a macro that will search the first sheet to see if any of the numbers on the second sheet match and then will take the cells that match and copy them below the one they matched. For example In the data below, if a receipt number on Sheet two matches a receipt number on sheet 1 then it would copy the line on sheet 2 to sheet 1 so that I would have all receipts together as seen below .

Sheet 1
Receipt Number Amount
12345 $5.00
87653 $7.00
95740 $10.00
92738 $5.00
287380 $6.00

Sheet 2
Receipt Number Amount
68706 $2.00
45687 $5.00
12345 $4.00
287380 $3.00
59778 $3.00

New Sheet 1
Receipt Number
12345 $5.00
12345 $4.00
87653 $7.00
95740 $10.00
92738 $5.00
287380 $6.00
287380 $3.00
 

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
Try:
Code:
Sub test()
    Application.ScreenUpdating = False
    Dim LastRow1 As Long
    LastRow1 = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim LastRow2 As Long
    LastRow2 = Sheets("Sheet2").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    Dim foundReceipt As Range
    For x = LastRow1 To 2 Step -1
        Set foundReceipt = Sheets("Sheet2").Range("A2:A" & LastRow2).Find(Sheets("Sheet1").Cells(x, 1), LookIn:=xlValues, LookAt:=xlWhole)
        If Not foundReceipt Is Nothing Then
            Sheets("Sheet1").Rows(x + 1).EntireRow.Insert
            foundReceipt.EntireRow.Copy Sheets("Sheet1").Range("A" & x + 1)
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I had a look at your file and I noticed that in both sheets there can be duplicate receipt numbers in column B. This presents a problem because I can't use the number to compare the rows between the two sheets. There has to be a unique identifier for each row that can be used to link the two sheets. There may be other ways to solve the problem but I can't think of any.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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