check for missing records and paste to sheet

si3po

Board Regular
Joined
Jan 7, 2019
Messages
98
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
hi all,

I have the following code:

Code:
     For iCounter = 9 To iLast
         Set rng1 = ws.Range("G9:G5000").Find(ws2.Range("G" & iCounter).Value)
         If rng1 Is Nothing Then
               ws2.Range("A" & iCounter & ":" & "K" & iCounter).Copy
               ws.Range("A" & ws.Range("A" & Application.Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteAll
          End If
     Next iCounter


It is designed to take the value from the 'iCounter row' in Col.G of Sheet3 and search Col.G of Sheet2 for it. If it doesn't match, the whole row in sheet3 is then copied to the last row of sheet2.

this works reasonably well for rows where a duplicate in Col.G of sheet 3 (actually an order number) is not found, however if a duplicate (two rows with the same order number) exists in sheet3 - for example split orders, or multiple items/suppliers for one order - the code only 'sees' the first instance in Sheet2 and so won't copy any addtional rows for duplicate order numbers and simply moves the iCounter to the next row on Sheet3.

can anyone help with this please??
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Is there another identifier?
On sheet 3 you have 2 records with the same order number.
On sheet2 you only have one record.
You want to copy a record of sheet3 and paste it into sheet2. But which of the 2 records should be pasted on sheet2?

Example:
Sheet3
AG
1Order Number
2123
3123

<tbody>
</tbody>


Sheet2
AG
1Order Number
2100
3120
4111
5123

<tbody>
</tbody>

Which record of sheet3 should I copy, record 2 or record 3?
 
Upvote 0
Is there another identifier?
On sheet 3 you have 2 records with the same order number.
On sheet2 you only have one record.
You want to copy a record of sheet3 and paste it into sheet2. But which of the 2 records should be pasted on sheet2?
QUOTE]

Thanks Dante,

i'll see if i can explain with an example below.

Sheet2
Order Number
Item Name
Qty
Customer
123
Screw,brass 25mm
100
Bob
456
Hook, brass
25
Frank
122
Nail, zinc 30mm
50
Bob
864
Nail, Steel 45mm
30
John

<tbody>
</tbody>


Sheet3
Order Number
Item Name
Qty
Customer
123
Screw,brass 25mm
100
Bob
456
Hook, brass
25
Frank
122
Nail, zinc 30mm
50
Bob
864
Nail, Steel 45mm
30
John
123
Screw, brass 50mm
150
Bob
456
Cap, Dust
250
Frank
456
Bit, Drill HSS 3mm
10
Frank

<tbody>
</tbody>


In this example, Sheet3 has extra lines with an Order Number that already exists on Sheet2, but for different items. When updating Sheet2, these extra lines should also be copied over. Currently they are skipped as the order number already exists.
 
Upvote 0
You forgot to put in which column each data is found.

Order Number - column G
Item Name - column ?
 
Upvote 0
You forgot to put in which column each data is found.

Order Number - column G
Item Name - column ?

Hi Dante,

I'm not sure that using the Item Name as an identifier will work, as it is plausible for me to have a single order number being fulfilled from multiple suppliers for the same item... a example would be that the customer places an order for, lets say 100 "Screws Bass, 50mm", but i only have 25 on my shelf stock. The remainder 75 will come into my stock from external source to satisfy this order.

To track whole order satisfaction and allow shipping of partial orders (e.g. the 25 from my stock) to the customer so that they are not kept waiting for an external supplier to deliver to me, the order is recorded as a single reference but with duplicates of that order for different quantites to the total requirement.

The only possible difference for each row in that instance (other than quantity, although this can be the same) would be what we call 'providers ref' - this is the Ext. Supplier order ref to us, but can often be blank until we get a notice from the suppliers. This value is plced into Col L.
 
Last edited:
Upvote 0
We can use 2 or more references, until we find unique records, it can be:

- Order Number - column G
- providers ref - Column L
- Item Name - column ?
- etc - Column ??
 
Upvote 0
We can use 2 or more references, until we find unique records, it can be:

- Order Number - column G
- providers ref - Column L
- Item Name - column ?
- etc - Column ??

That might work, using the customer ID would help also i think. The fields are a follows

- Order Number - Column G
- providers ref - Column L
- Item Name - Column D
- Cust ID - Column J
 
Upvote 0
Try this, replace your code with the following.

Code:
  Dim c As Range, r As Range, f As Range, cell As String, exists As Boolean
  Set r = ws.Range("G9:G5000")
  For iCounter = 9 To iLast
    exists = False
    Set f = r.Find(ws2.Range("G" & iCounter).Value, , xlValues, xlWhole)
    If Not f Is Nothing Then
        cell = f.Address
        Do
          If ws2.Range("G" & iCounter).Value = ws.Range("G" & f.Row).Value And _
             ws2.Range("L" & iCounter).Value = ws.Range("L" & f.Row).Value And _
             ws2.Range("D" & iCounter).Value = ws.Range("D" & f.Row).Value And _
             ws2.Range("J" & iCounter).Value = ws.Range("J" & f.Row).Value Then
             exists = True
             Exit Do
          End If
          Set f = r.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
    End If
    If exists = False Then
      ws2.Range("A" & iCounter & ":" & "L" & iCounter).Copy
      ws.Range("A" & ws.Range("A" & Application.Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteAll
    End If
  Next iCounter
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

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