Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I have worked out a UDF which replicates Excel Rank() function. Now, I am trying to modify it such that instead of outputting 1 2 2 4 as the ranks for a set {2,5,5,8} it should output 1 3 3 4; and it should be able to handle any data as per the modified logic.
Code:
Public Function Rnk( _
ByVal x As Variant, _
ByRef y As Variant, _
ByVal z As Boolean) As Variant
' x is the item, y is the Range, and z is the rank order.
If TypeName(y) = "Range" Then
y = y.Value2
Else
End If
With CreateObject("System.Collections.ArrayList")
Dim i As Variant
For Each i In y
If VarType(i) >= 2# And VarType(i) <= 6# Then .Add Val(i)
Next i
.Sort
If z = False Then ' Descending Order
.Reverse
Else ' Ascending Order
End If
Rnk = Application.WorksheetFunction.Match(x, .ToArray, 0#) + 1
End With
End Function