This is the solution to my problem from Mr.excel.com
Sub Kelly()
Dim d As Object
Dim m As Long
Dim ary
Application.ScreenUpdating = False
m = Range("D" & Rows.Count).End(xlUp).Row - 6 ' 6 because my data starts from row 7
ary = Split("100 95 90") ' system reserved numbers for my rank
ary = Application.Transpose(Application.Transpose(ary))
Set d = CreateObject("scripting.dictionary")
For i = 1 To UBound(ary)
d(CLng(ary(i))) = Empty
Next
Call toRank(m, d, 4, 13, ary)
ary = Split("1000 950 900") 'system reserved for my totals ranking
ary = Application.Transpose(Application.Transpose(ary))
Set d = CreateObject("scripting.dictionary")
For i = 1 To UBound(ary)
d(CLng(ary(i))) = Empty
Next
Call toRank(m, d, 14, 14, ary)
Application.ScreenUpdating = True
End Sub
Sub toRank(m As Long, d As Object, a As Long, b As Long, ary As Variant)
'https://www.mrexcel.com/forum/excel-questions/1113655-ranking-code-amendment-system-reserved-numbers-vba.html
Dim i As Long, z As Long, n As Long
Dim e As Object, f As Object
Dim arz
Dim c As Range
For g = a To b
arb = Application.Transpose(Cells(7, g).Resize(m)) ' 7 because my data started from row 7
ReDim arz(1 To UBound(arb))
For i = 1 To UBound(arb)
arz(i) = WorksheetFunction.Large(arb, i)
Next i
n = arz(UBound(arz))
Set e = CreateObject("scripting.dictionary")
For i = 1 To UBound(arz)
e(arz(i)) = Empty
Next
Set f = CreateObject("scripting.dictionary")
z = 1
For i = ary(1) To n Step -1
If d.Exists(i) And Not e.Exists(i) Then
z = z + 1
End If
' If e.Exists(i) Then f(i) = z: z = z + 1
If e.Exists(i) Then f(i) = z & GetOrdinalSuffixForRank(z): z = z + 1
Next
For Each c In Cells(7, g).Resize(m) ' 7 for row 7
If f.Exists(c.Value) Then c.Offset(, 12) = f(c.Value)
Next
Next
End Sub
Function GetOrdinalSuffixForRank(Rnk As Long) As String
Dim sSuffix$
If Rnk Mod 100 >= 11 And Rnk Mod 100 <= 20 Then
sSuffix = "th"
Else
Select Case (Rnk Mod 10)
Case 1: sSuffix = "st"
Case 2: sSuffix = "nd"
Case 3: sSuffix = "rd"
Case Else: sSuffix = "th"
End Select
End If
GetOrdinalSuffixForRank = sSuffix
End Function
Credit:
Akuini
Bookmarks