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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,525
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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,525
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.
 

Forum statistics

Threads
1,082,284
Messages
5,364,281
Members
400,787
Latest member
bs04c

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