Public Function getRanking(ByVal theScore As Long, Optional theDirection As String = "Desc")
Static oldScore As Long, theRanking As Long, thePosition As Long, usePositionInstead As Boolean
' theScore: These should be a set of sorted scores
' theDirection: are the Scores Ascending or Descending? Default is Descending
theDirection = LCase(Left(theDirection, 1))
If theRanking < 0 Then theRanking = 0
If thePosition < 0 Then thePosition = 0
' If theDirection is Descending and the Score is lower than the previous then goto NoReset
' If theDirection is Ascending and the Score is higher than the previous then goto NoReset
If (theDirection = "d") And (oldScore >= theScore) Then GoTo NoReset
If (theDirection = "a") And (oldScore <= theScore) Then GoTo NoReset
' If theDirection is Descending and the Score is higher than the previous then reset the variables
' If theDirection is Ascending and the Score is lower than the previous then reset the variables
thePosition = 0
theRanking = 0
' Set usePositionInstead = false
usePositionInstead = False
NoReset:
' Update thePosition
thePosition = thePosition + 1
If oldScore <> theScore Then
' If the OldScore is not the same as theScore then
' Make oldScore = theScore
' Update the ranking
oldScore = theScore
' Increment theTarnking
theRanking = theRanking + 1
' If usePositionInstead is true then make theRanking = thePosition instead
If usePositionInstead Then theRanking = thePosition
' Set usePositionInstead = false
usePositionInstead = False
Else
' Don't increment theTarnking
theRanking = theRanking + 0
' Set usePositionInstead = true
usePositionInstead = True
End If
' Set getRanking = theRanking
getRanking = theRanking
End Function