Getting position of an item in a list

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
sorry title doesn't really explain it well:

I have data like below (100k + rows)

Item CodeSellerPrice
FR11ABCTech16.49
FR11KLG15.19
FR11JustPro15.11
FR11MapLtd14.99
GR44KLG38.99
GR44ABCTech36.49
GR44JustPro35.99
ZD16KLG12.99
ZD16JustPro12.89
ZD16ABCTech12.49
PG20JustPro22.79
PG20ABCTech21.99
PG20MapLtd19.99
PG20KLG18.49

It is always sorted like above (grouped by item, then by price high to low)

I need to find out the following:
Which Position JustPro is in the list and price difference from 1st position

From the above - expected output is:

Item CodePositionPrice Difference
FR1131.38
GR4433
ZD1620.1
PG2010

also if possible to calculate the price difference between 1st + 2nd for every item

doable with just formulas ?

appreciate any help
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try:

Book1
ABCDEFG
1Item CodeSellerPriceItem CodePositionPrice Difference
2FR11ABCTech16.49FR1131.38
3FR11KLG15.19GR4433
4FR11JustPro15.11ZD1620.1
5FR11MapLtd14.99PG2010
6GR44KLG38.99
7GR44ABCTech36.49
8GR44JustPro35.99
9ZD16KLG12.99
10ZD16JustPro12.89
11ZD16ABCTech12.49
12PG20JustPro22.79
13PG20ABCTech21.99
14PG20MapLtd19.99
15PG20KLG18.49
Sheet5
Cell Formulas
RangeFormula
F2:F5F2=MATCH(E2&"JustPro",$A$2:$A$15&$B$2:$B$15,0)-MATCH(E2,$A$2:$A$15,0)+1
G2:G5G2=VLOOKUP(E2,$A$2:$C$15,3,0)-SUMIFS($C$2:$C$15,$A$2:$A$15,E2,$B$2:$B$15,"JustPro")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
What version of Excel are you using?
Please update your account details to show this, as it affects which functions you can use.

Another option that might work, depending on your version
+Fluff v2.xlsm
ABCDEFG
1Item CodeSellerPriceItem CodePositionPrice Difference
2FR11ABCTech16.49FR1131.38
3FR11KLG15.19GR4433
4FR11JustPro15.11ZD1620.1
5FR11MapLtd14.99PG2010
6GR44KLG38.99
7GR44ABCTech36.49
8GR44JustPro35.99
9ZD16KLG12.99
10ZD16JustPro12.89
11ZD16ABCTech12.49
12PG20JustPro22.79
13PG20ABCTech21.99
14PG20MapLtd19.99
15PG20KLG18.49
16
Summary
Cell Formulas
RangeFormula
F2:F5F2=MATCH(E2&"JustPro",$A$2:$A$15&$B$2:$B$15,0)-MATCH(E2,$A$2:$A$15,0)+1
G2:G5G2=IF(F2=1,0,MAXIFS($C$2:$C$15,$A$2:$A$15,E2)-SUMIFS($C$2:$C$15,$A$2:$A$15,E2,$B$2:$B$15,"JustPro"))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Ok, with 365 some other options, depending on which build your on.
+Fluff v2.xlsm
ABCDEFGH
1Item CodeSellerPriceItem CodePositionPrice DifferenceWith Let
2FR11ABCTech16.49FR1131.381.38
3FR11KLG15.19GR44333
4FR11JustPro15.11ZD1620.10.1
5FR11MapLtd14.99PG20100
6GR44KLG38.99
7GR44ABCTech36.49
8GR44JustPro35.99
9ZD16KLG12.99
10ZD16JustPro12.89
11ZD16ABCTech12.49
12PG20JustPro22.79
13PG20ABCTech21.99
14PG20MapLtd19.99
15PG20KLG18.49
Summary
Cell Formulas
RangeFormula
F2:F5F2=XMATCH("JustPro",FILTER($B$2:$B$15,$A$2:$A$15=E2),0)
G2:G5G2=IF(F2=1,0,INDEX(FILTER($C$2:$C$15,$A$2:$A$15=E2),1)-INDEX(FILTER($C$2:$C$15,$A$2:$A$15=E2),F2))
H2:H5H2=LET(Fltr,FILTER($C$2:$C$15,$A$2:$A$15=E2),IF(F2=1,0,INDEX(Fltr,1)-INDEX(Fltr,F2)))
 
Upvote 0
Solution
VBA Code:
Sub test()
    Dim sat&, sira&, i&, al$, eski$
    sat = 2
    eski = ""
    Range("d2:f" & Rows.Count).ClearContents
    With CreateObject("Scripting.Dictionary")
        For i = 2 To Cells(Rows.Count, 1).End(3).Row
            al = Cells(i, 1).Value
            If eski <> al Then
                If Not .exists(al) Then
                    Cells(sat, "d").Value = al
                    Cells(sat, "f").Value = Cells(i, 3).Value
                    .Item(al) = sat
                    sat = sat + 1
                End If
                sira = .Item(al)
                Cells(sira, "e").Value = Cells(sira, "e").Value + 1
                If Cells(i, 2).Value = "JustPro" Then
                    Cells(sira, "f").Value = Cells(sira, "f").Value - Cells(i, "c").Value
                    eski = al
                End If
            End If
        Next i
    End With
End Sub
 
Upvote 0
Another option
VBA Code:
Sub JumboCactur()
   Dim Ary As Variant, Tmp As Variant
   Dim r As Long
   
   Ary = Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row).Value2
   
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            If Ary(r, 2) = "JustPro" Then
               .Add Ary(r, 1), Array(1, 0)
            Else
               .Add Ary(r, 1), Array(r, Ary(r, 3))
            End If
         ElseIf Ary(r, 2) = "JustPro" Then
            Tmp = .Item(Ary(r, 1))
            Tmp(0) = r - Tmp(0) + 1
            Tmp(1) = Tmp(1) - Ary(r, 3)
            .Item(Ary(r, 1)) = Tmp
         End If
      Next r
      Range("E2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("F2").Resize(.Count, 2).Value = Application.Index(.Items, 0)
   End With
End Sub
This will also create the list of Item codes in col E
 
Upvote 0
Another option
VBA Code:
Sub JumboCactur()
   Dim Ary As Variant, Tmp As Variant
   Dim r As Long
  
   Ary = Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row).Value2
  
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            If Ary(r, 2) = "JustPro" Then
               .Add Ary(r, 1), Array(1, 0)
            Else
               .Add Ary(r, 1), Array(r, Ary(r, 3))
            End If
         ElseIf Ary(r, 2) = "JustPro" Then
            Tmp = .Item(Ary(r, 1))
            Tmp(0) = r - Tmp(0) + 1
            Tmp(1) = Tmp(1) - Ary(r, 3)
            .Item(Ary(r, 1)) = Tmp
         End If
      Next r
      Range("E2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("F2").Resize(.Count, 2).Value = Application.Index(.Items, 0)
   End With
End Sub
This will also create the list of Item codes in col E

Thanks @Fluff Though with large dataset it comes out wrong
maybe if "JustPro" isn't found for an item which is in some cases - that could be breaking your code ?
if "JustPro" not found i just want to output 0 / 0 for column F/G
 
Upvote 0
How about
VBA Code:
Sub JumboCactur()
   Dim Ary As Variant, Tmp As Variant
   Dim r As Long
   Dim Found As Boolean
   
   Ary = Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row).Value2
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            If Ary(r, 2) = "JustPro" Then
               Found = True
               .Add Ary(r, 1), Array(1, 0)
            Else
               .Add Ary(r, 1), Array(r, Ary(r, 3))
            End If
         ElseIf Ary(r, 2) = "JustPro" Then
            Found = True
            Tmp = .Item(Ary(r, 1))
            Tmp(0) = r - Tmp(0) + 1
            Tmp(1) = Tmp(1) - Ary(r, 3)
            .Item(Ary(r, 1)) = Tmp
         End If
         If r = UBound(Ary) Then
            If Not Found Then .Item(Ary(r, 1)) = Array(0, 0)
         ElseIf Ary(r, 1) <> Ary(r + 1, 1) Then
            If Not Found Then .Item(Ary(r, 1)) = Array(0, 0)
            Found = False
         End If
      Next r
      Range("E2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("F2").Resize(.Count, 2).Value = Application.Index(.Items, 0)
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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