Evaluate data for any price changes

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
857
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to find a way to easily know if any price changes from a very large dataset. Over 100k rows. Currently I am filtering by frequency, then using pivot tables and its cumbersome. I am looking if I can just have a column called "changes" so I only have to focus on any duplicates that have changes. There could be a lot of duplicates with the same price but I don't really care about those. The unique Identifier column A is what sets them apart. I did a snipbit below using XLBB to give a frame of reference what I am dealing with. In the future I will probably looking for a way to give me the changes but for right now babysteps and just looking for changes "Yes or No". Thank you as always for helping look at this.

Book1
ABCDEFGHIJ
5Not really needed if I have column J workingHELP
6IdentifierUnique IdentifierGroupGroup Class Price SKUFileFile NameFrequencyAny Change?
7AAA123456789Team11$ 10.00SKUAAANonFXXXXXXX12
8BBB987654321Team12$ 10.50SKUBBBNonFXXXXXXX11
9CCC112233446Team21$ 75.00SKUCCCNonFXXXXXXX11
10DDD124512566Team22$ 69.80SKUDDDNonFXXXXXXX11
11EEE122456789Team23$ 100.25SKUEEENonFXXXXXXX12
12FFF324512566Team34$ 100.00SKUFFFNonFXXXXXXX12
13GGG924512566Team35$ 74.00SKUGGGNonFXXXXXXX12
14HHH712233446Team498$ 9.00SKUHHHNonFXXXXXXX14
15III522456789Team598$ 5.60SKUIIINonFXXXXXXX13
16LLL624512566Team698$ 7.50SKULLLNonFXXXXXXX13
17AAA123456789Team11$ 9.00SKUAAANonFXXXXXXX12
18EEE122456789Team23$ 5.00SKUEEENonFXXXXXXX12
19FFF324512566Team34$ 7.00SKUFFFNonFXXXXXXX12
20GGG924512566Team35$ 74.00SKUGGGNonFXXXXXXX12
21HHH712233446Team498$ 9.00SKUHHHNonFXXXXXXX14
22III522456789Team598$ 5.60SKUIIINonFXXXXXXX13
23LLL624512566Team698$ 4.00SKULLLNonFXXXXXXX13
24HHH712233446Team498$ 9.00SKUHHHNonFXXXXXXX14
25III522456789Team598$ 5.60SKUIIINonFXXXXXXX13
26LLL624512566Team698$ 7.50SKULLLNonFXXXXXXX13
27HHH712233446Team498$ 10.00SKUHHHNonFXXXXXXX14
28III522456789Team598$ 5.60SKUIIINonFXXXXXXX13
29LLL624512566Team698$ 111.00SKULLLNonFXXXXXXX13
30LLL624512566Team698$ 123.00SKULLLNonFXXXXXXX13
Sheet1
Cell Formulas
RangeFormula
F7:F30F7="SKU"&A7
I7:I30I7=COUNTIF($A$7:$A$27,A7)
 
I've optimized the code further; less looping. I've tested on 100K rows. It takes me an average of 1.5 seconds per run.
VBA Code:
Sub PriceChanges5()
    Dim id, price, output
    Dim i As Long, numRow As Long
    Dim ws As Worksheet
    Dim uniquePrices As Object, idPrices As Object, firstInstance As Object
    Set uniquePrices = CreateObject("Scripting.Dictionary")
    Set idPrices = CreateObject("Scripting.Dictionary")
    Set firstInstance = CreateObject("Scripting.Dictionary")
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    numRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    id = ws.Range("A6:A" & numRow).Value2
    price = ws.Range("E6:E" & numRow).Value2
    ReDim output(1 To numRow, 1 To 1)
    Dim t As Double: t = Timer
   
    ' Loop through the data to populate the dictionaries
    For i = 1 To numRow - 5
        If Not idPrices.exists(id(i, 1)) Then
            idPrices.Add id(i, 1), CreateObject("Scripting.Dictionary")
        End If
        If Not idPrices(id(i, 1)).exists(price(i, 1)) Then
            idPrices(id(i, 1)).Add price(i, 1), True
        End If
    Next i
    ' Loop through the data again to populate the output array
    For i = 2 To numRow - 5
        If idPrices(id(i, 1)).Count = 1 Then
            output(i - 1, 1) = "No" ' Only one price for this ID
        ElseIf idPrices(id(i, 1)).Count > 1 Then
            ' Multiple distinct prices for this ID
            If Not firstInstance.exists(id(i, 1) & price(i, 1)) Then
                output(i - 1, 1) = "Yes" ' Mark the first instance as "Yes"
                firstInstance.Add id(i, 1) & price(i, 1), True
            Else
                output(i - 1, 1) = "No" ' Mark subsequent instances as "No"
            End If
        End If
    Next i
    ' Output the result to the worksheet
    ws.Range("J7").Resize(UBound(output, 1), 1).Value = output
    ' Apply filter and sort
    With ws.Range("A6:J" & numRow)
        .AutoFilter Field:=10, Criteria1:="Yes" 'Field 10 = Column J
        .Sort key1:=Range("A6"), order1:=xlAscending, Header:=xlYes
        .SpecialCells(xlCellTypeVisible).Copy ws.Range("Q6")
        .AutoFilter
    End With
    MsgBox Timer - t 'Check how long the code runs.
End Sub
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I plan to test this today. before i start if I wanted to change the output say to a tab called "changes" would the below modification look ok to you? I didn't want to break your code even before trying for the first time on the big data set :)

VBA Code:
Set ws1 = ThisWorkbook.Worksheets("Changes")

VBA Code:
    ' Output the result to the worksheet
    ws1.Range("A2").Resize(UBound(output, 1), 1).Value = output
    ' Apply filter and sort
    With ws1.Range("A1:J" & numRow)
        .AutoFilter Field:=10, Criteria1:="Yes" 'Field 10 = Column J
        .Sort key1:=Range("A6"), order1:=xlAscending, Header:=xlYes
        .SpecialCells(xlCellTypeVisible).Copy ws.Range("Q6")
        .AutoFilter
    End With
 
Upvote 0
1) You should test on a COPY, not the original.
2) To change the location output see the red below. Everything else is for Sheet1", the source data, so keep them as is.
Rich (BB code):
    ' Output the result to the worksheet

    ws.Range("J7").Resize(UBound(output, 1), 1).Value = output

    ' Apply filter and sort

    With ws.Range("A6:J" & numRow)

        .AutoFilter Field:=10, Criteria1:="Yes" 'Field 10 = Column J

        .Sort key1:=Range("A6"), order1:=xlAscending, Header:=xlYes

        .SpecialCells(xlCellTypeVisible).Copy ws1.Range("A1")

        .AutoFilter

    End With
 
Upvote 0
Okay thanks - i think I got it going with my small set. Even my small set came at 5 seconds. I don't think my large data set should be that bad though to notice. Will look at that later today the file comes nightly at 6PM EST

@Cubist you are raising another issue of mine I posted previously where I use similar formulas (sort, unique, filter) but I have a crazy amount of performance issues (1 time it took 1 day to run), because I have a lot of data. Your expertise may be helpful there :) Formula to match criteria but then ended up stuck here so I wasn't able to bring that tool forward Find last row of sheet based off cell value to reference the sheet name
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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