Getting the last price for each duplicates ID

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I would show report in OUTPUT sheet by brings batches from column C and brings the last price for each duplicates batch from column E,F for each sheet.
Microsoft‫‬.xlsx
ABCDEF
1ITEMDATEBATCHCUSPRICEPRICE1
2101/01/2022BTS00CVF-00111.00155.00
3201/01/2022BTS01CVF-01110.00115.00
4301/01/2022BTS02CVF-02221.00250.00
5401/01/2022BTS03CVF-03155.00
6501/01/2022BTS01CVF-04120.00140.00
7601/01/2022BTS02CVF-05115.00120.00
8701/01/2022BTS00CVF-06114.00120.00
9803/01/2023BTS01CVF-07122.00144.00
10904/01/2023BTS02CVF-08120.00177.00
111005/01/2023BTS00CVF-09120.00160.00
121103/02/2023BTS01CVF-10108.00139.00
131204/02/2023BTS02CVF-11120.00177.00
141305/02/2023BTS00CVF-12115.00122.00
151406/02/2023BTS04CVF-13100.00110.00
161507/02/2023BTS05CVF-14120.00125.00
171608/02/2023BTS06CVF-15115.00120.00
PRICES


Microsoft.xlsx
ABCDEF
1ITEMDATEBATCHCNNSSLSSL1
2101/01/2022BTS02CVF-000150.00130.00
3201/01/2022BTS00CVF-001170.00200.00
4303/01/2023BTS01CVF-002155.00160.00
5401/01/2022BTS03CVF-003170.00220.00
6501/01/2022BTS00CVF-004160.00180.00
7601/01/2022BTS01CVF-005140.00150.00
8701/01/2022BTS02CVF-006221.00250.00
9801/01/2022BTS03CVF-007166.00170.00
10901/01/2022BTS00CVF-008140.00145.00
111001/01/2022BTS01CVF-009130.00144.00
121101/01/2022BTS02CVF-010221.00250.00
131207/02/2023BTS05CVF-011125.00
141308/02/2023BTS06CVF-012120.00130.00
151408/02/2023BTS06CVF-013122.00123.00
SSL


and report should be

Microsoft‫‬.xlsx
ABCDEF
1ITEMBATCHPRICEPRICE1SSLSSL1
21BTS00115.00122.00140.00145.00
32BTS01108.00139.00130.00144.00
43BTS02120.00177.00221.00250.00
54BTS03155.00166.00170.00
65BTS04100.00110.00
76BTS05120.00125.00125.00
87BTS06115.00120.00122.00123.00
OUTPUT

should replace data when every time running the macro.
I have about 1500 batches and 8000 rows when repeat data for batches
thanks
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Use this code
PHP:
Option Explicit
Sub TEST()
Dim i&, j&, k&, rng, res(1 To 10000, 1 To 6), c As Boolean
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")
With Sheets("PRICES")
    rng = .Range("A1").CurrentRegion.Value ' assume A1 have data
    For i = UBound(rng) To 2 Step -1 'assume date is in creasing. loop from bottom and get the 1st unique batch found
        If Not dic.exists(rng(i, 3)) Then ' if 1st found then store in an array
            dic.Add rng(i, 3), ""
            k = k + 1: res(k, 1) = k: res(k, 2) = rng(i, 3)
            res(k, 3) = rng(i, 5): res(k, 4) = rng(i, 6)
        End If
    Next
End With
dic.RemoveAll
With Sheets("SSL")
    rng = .Range("A1").CurrentRegion.Value
    For i = UBound(rng) To 2 Step -1
        If Not dic.exists(rng(i, 3)) Then
            dic.Add rng(i, 3), "": c = False
            For j = 1 To k
                If res(j, 2) = rng(i, 3) Then
                    c = True: res(j, 5) = rng(i, 5): res(j, 6) = rng(i, 6)
                End If
            Next
            If Not c Then
                k = k + 1: res(k, 1) = k: res(k, 2) = rng(i, 3)
                res(k, 5) = rng(i, 5): res(k, 6) = rng(i, 6)
            End If
        End If
    Next
End With
Sheets("OUTPUT").Activate
Set dic = Nothing
If k = 0 Then Exit Sub
Range("A2:F10000").ClearContents
Range("A2").Resize(k, 6).Value = res
Range("B2:F" & k + 1).Sort key1:=Range("B1")
End Sub
 
Upvote 0
Solution
Awesome !
this is exactly what I want it.(y)
thank you.:love::love:
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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