| Group | Product | Value | Ranking |
|---|---|---|---|
| Group A | Salmon | 2000 | 1 |
| Group A | Trout | 900 | 2 |
| Group B | Pencil | 500 | 2 |
| Group B | Rubber | 700 | 1 |
| Group C | Smirnoff Vodka | 1200 | 1 |
| Group C | Heineken | 900 | 2 |
I'm trying to achieve "Ranking by use of Sumproduct formula" in each group, resulting in the "Value" in each group to have an ascending order.
I can do it in excel as a formula:
=SUMPRODUCT(($A$2:$A$10000=A2)*($C$2:$C$10000>C2))+1
However, I want to achieve that in VBA; I tried to do it but it still shows a bug in the code below (For this one I need a for loop version until the lastrow):
Sub Ranking()
lastrowA = Sheet1.Range("A" & Rows.Count).End(xlUp).Row 'Find a lastrow of column A
Dim i As Integer
Dim groupRange As Range
Dim productRange As Range
Set groupRange = Sheet1.Range("A:A" & lasrowA) 'Cost Center Name Range
Set productRange = Sheet1.Range("B:B" & lasrowA) 'Store Name Range
For i = 2 To lastrowA
Cells(i, 4).Value = Evaluate("Sumproduct((A2:A10000=Cells(i,1))*(A2:A10000>Cells(i,2)))") + 1
Next i
End Sub