Ranking Code Amendment With System Reserved Numbers -vba

Status
Not open for further replies.

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I want to set some numbers as system numbers to use against the ranking as done by the code below. Currently, I am using three numbers – 100, 95 and 90. In the future, I may reduce (say 2 numbers) or increase (say 5 or more numbers). And in each scenario, the aim is to make those numbers take the top-most ranks, should those numbers fail to appear in my data as I define the rules below. So these are the rules:
1. 100 is always ranked first. Which means that if there is no 100 in my data, then the highest number in my data is second but not first and run for numbers from 99 to 95 in my data
2. If there are no numbers from 99 to 95 in my data, then 95 is ranked second into the system, then numbers from 94 to 91 take from 4th downwards. But if there are say 98 and 96 in my data, then after systematically using the first rank for 100(system number), then we have 2nd for 98 and 3rd for 96. In this case, the next system number, 95, which by default was supposed to be 2nd, will now shift to the 4th rank. So here, the rank shift as there are more numbers in between.
3. If the highest ranked number in the point 2 above is 2nd, then, we assign 3rd to our system number 90. But if that is different, say 3rd, 4th etc, then we make the rank shift as described in point 2 above.


Sample of how I want my output look like
Code:
==================
Number        Rank
==================
98        2nd
96        3nd
94        5th
93        6th
70        8th 
=================
From the above, since there were no 100, 95 and 90 in my data, those ranks were absorbed by the system, 1st for 100, 4th for 95 and 7th for 90.


Thanks so much for your time and effort to help me out.


Code:
Sub RankDynamic()
     Dim dicSection As Object, vItem As Variant, wsData As Worksheet, vSection As Variant, rScore As Range, _
     rCell As Range, Score As Variant, Rnk As Double, LastRow&, iCol&
     Application.ScreenUpdating = False
     
     Set wsData = Sheets("Sheet1")
    With wsData
        If .FilterMode Then .ShowAllData
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
     End With
     
    If LastRow > 1 Then
         'Data exists
    Else
        MsgBox "No data exists!", vbExclamation
        Exit Sub
    End If
    
    On Error Resume Next
    Set dicSection = CreateObject("Scripting.Dictionary")
    dicSection.CompareMode = 1 'vbTextCompare
    vSection = wsData.Range("C6:C" & LastRow).Value
    For i = LBound(vSection) + 1 To UBound(vSection)
        If Not dicSection.Exists(vSection(i, 1)) Then
            dicSection(vSection(i, 1)) = ""
        End If
     Next i
For Each vItem In dicSection.keys()
    With wsData.UsedRange
    .AutoFilter field:=3, Criteria1:=vItem
    
    Set rScore = .Offset(1, 1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
    For i = 2 To 12
        For Each rCell In rScore.Offset(, i)
        Score = rCell.Value
            If Application.IsNumber(Score) Then
                Rnk = WorksheetFunction.Rank(CDbl(Score), rScore.Offset(, i))
                rCell.Offset(, 12).Value = Rnk & GetOrdinalSuffixForRank(Rnk)
            End If
        Next rCell
    Next i
        .AutoFilter
    End With
    Next vItem
    Application.ScreenUpdating = True
    
    Set dicSection = Nothing
    Set rScore = Nothing
    Set rCell = Nothing
End Sub


Function GetOrdinalSuffixForRank(Rnk As Double) 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
 
So, what's the problem?
You can sort the data as desired and then run the code. The code will work whether the data is sorted or not sorted.
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
The problem is, it ranks all of them together.

But from the table I posted, all X labeled data are ranked differently.

That's seen from R2 upwards from post #20
 
Upvote 0
Try this:
Code:
[FONT=Lucida Console][COLOR=Royalblue]Sub[/COLOR] Kelly()
[COLOR=Royalblue]Dim[/COLOR] d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] m [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], qr [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], qq [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] ary


Application.ScreenUpdating = False
qq = Range([COLOR=Darkcyan]"D"[/COLOR] & Rows.Count).[COLOR=Royalblue]End[/COLOR](xlUp).Row

[COLOR=Royalblue]For[/COLOR] qr = [COLOR=Brown]7[/COLOR] [COLOR=Royalblue]To[/COLOR] qq
    
    m = WorksheetFunction.CountIf(Range([COLOR=Darkcyan]"C7:C"[/COLOR] & qq), Range([COLOR=Darkcyan]"C"[/COLOR] & qr))  'case insensitive
        
        ary = Split([COLOR=Darkcyan]"[COLOR=Brown]100[/COLOR] [COLOR=Brown]95[/COLOR] [COLOR=Brown]90[/COLOR]"[/COLOR])
        ary = Application.Transpose(Application.Transpose(ary))
        [I][COLOR=Dimgray]'ary = Application.Transpose(Range("O2", Cells(Rows.Count, "O").End(xlUp)))[/COLOR][/I]
        
        [COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
            [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](ary)
                d([COLOR=Royalblue]CLng[/COLOR](ary(i))) = [COLOR=Royalblue]Empty[/COLOR]
            [COLOR=Royalblue]Next[/COLOR]
        [COLOR=Royalblue]Call[/COLOR] toRank(qr, m, d, [COLOR=Brown]4[/COLOR], [COLOR=Brown]13[/COLOR], ary)
        
        
        ary = Split([COLOR=Darkcyan]"[COLOR=Brown]1000[/COLOR] [COLOR=Brown]950[/COLOR] [COLOR=Brown]900[/COLOR]"[/COLOR])
        ary = Application.Transpose(Application.Transpose(ary))
        [I][COLOR=Dimgray]'ary = Application.Transpose(Range("AA2", Cells(Rows.Count, "AA").End(xlUp)))[/COLOR][/I]
        
        [COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
            [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](ary)
                d([COLOR=Royalblue]CLng[/COLOR](ary(i))) = [COLOR=Royalblue]Empty[/COLOR]
            [COLOR=Royalblue]Next[/COLOR]
        [COLOR=Royalblue]Call[/COLOR] toRank(qr, m, d, [COLOR=Brown]14[/COLOR], [COLOR=Brown]14[/COLOR], ary)

    qr = qr + m - [COLOR=Brown]1[/COLOR]

[COLOR=Royalblue]Next[/COLOR] qr

Application.ScreenUpdating = True

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR]

[COLOR=Royalblue]Sub[/COLOR] toRank(qr [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], m [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], a [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], b [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], ary [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Variant[/COLOR])
[I][COLOR=Dimgray]'https://www.mrexcel.com/forum/excel-questions/1113655-ranking-code-amendment-system-reserved-numbers-vba.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], z [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] e [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], f [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] arz
[COLOR=Royalblue]Dim[/COLOR] c [COLOR=Royalblue]As[/COLOR] Range


[COLOR=Royalblue]For[/COLOR] g = a [COLOR=Royalblue]To[/COLOR] b

    arb = Application.Transpose(Cells(qr, g).Resize(m))
    [COLOR=Royalblue]ReDim[/COLOR] arz([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb))
        
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb)
            arz(i) = WorksheetFunction.Large(arb, i)
             
        [COLOR=Royalblue]Next[/COLOR] i
    
    n = arz([COLOR=Royalblue]UBound[/COLOR](arz))
    
    [COLOR=Royalblue]Set[/COLOR] e = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arz)
            e(arz(i)) = [COLOR=Royalblue]Empty[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
    [COLOR=Royalblue]Set[/COLOR] f = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        z = [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]For[/COLOR] i = ary([COLOR=Brown]1[/COLOR]) [COLOR=Royalblue]To[/COLOR] n [COLOR=Royalblue]Step[/COLOR] -[COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]If[/COLOR] d.Exists(i) [COLOR=Royalblue]And[/COLOR] [COLOR=Royalblue]Not[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR]
            z = z + [COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[I][COLOR=Dimgray]'            If e.Exists(i) Then f(i) = z: z = z + 1[/COLOR][/I]
            [COLOR=Royalblue]If[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR] f(i) = z & GetOrdinalSuffixForRank(z): z = z + [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] c In Cells(qr, g).Resize(m)
            [COLOR=Royalblue]If[/COLOR] f.Exists(c.Value) [COLOR=Royalblue]Then[/COLOR] c.Offset(, [COLOR=Brown]12[/COLOR]) = f(c.Value)
        [COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Great!!!

You are unstoppable, @Akuini.

Very brilliant.

This line:

Code:
[FONT='inherit'][COLOR=Royalblue]ReDim[/COLOR] arz([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb))[/FONT]

When I have only one row for a category, say only one row for category X, it runs a mismatch error.

That won't be much of a problem ATM. But for the future, I will be glad to know how to bypass that, when there appear to be only one record for a given category.

Regards..
 
Upvote 0
When I have only one row for a category, say only one row for category X, it runs a mismatch error.
Ok, use this one:

Code:
[FONT=Lucida Console][COLOR=Royalblue]Sub[/COLOR] toRank(qr [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], m [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], a [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], b [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], ary [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Variant[/COLOR])
[I][COLOR=Dimgray]'https://www.mrexcel.com/forum/excel-questions/1113655-ranking-code-amendment-system-reserved-numbers-vba.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], z [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] e [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], f [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] arz, arb
[COLOR=Royalblue]Dim[/COLOR] c [COLOR=Royalblue]As[/COLOR] Range


[COLOR=Royalblue]For[/COLOR] g = a [COLOR=Royalblue]To[/COLOR] b

[COLOR=Royalblue]If[/COLOR] m = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]Then[/COLOR]
    [COLOR=Royalblue]ReDim[/COLOR] arb([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Brown]1[/COLOR])
    arb([COLOR=Brown]1[/COLOR]) = Cells(qr, g)
[COLOR=Royalblue]Else[/COLOR]
    arb = Application.Transpose(Cells(qr, g).Resize(m))
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]

    [COLOR=Royalblue]ReDim[/COLOR] arz([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb))
        
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb)
            arz(i) = WorksheetFunction.Large(arb, i)
             
        [COLOR=Royalblue]Next[/COLOR] i
    
    n = arz([COLOR=Royalblue]UBound[/COLOR](arz))
    
    [COLOR=Royalblue]Set[/COLOR] e = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arz)
            e(arz(i)) = [COLOR=Royalblue]Empty[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
    [COLOR=Royalblue]Set[/COLOR] f = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        z = [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]For[/COLOR] i = ary([COLOR=Brown]1[/COLOR]) [COLOR=Royalblue]To[/COLOR] n [COLOR=Royalblue]Step[/COLOR] -[COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]If[/COLOR] d.Exists(i) [COLOR=Royalblue]And[/COLOR] [COLOR=Royalblue]Not[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR]
            z = z + [COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[I][COLOR=Dimgray]'            If e.Exists(i) Then f(i) = z: z = z + 1[/COLOR][/I]
            [COLOR=Royalblue]If[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR] f(i) = z & GetOrdinalSuffixForRank(z): z = z + [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] c In Cells(qr, g).Resize(m)
            [COLOR=Royalblue]If[/COLOR] f.Exists(c.Value) [COLOR=Royalblue]Then[/COLOR] c.Offset(, [COLOR=Brown]12[/COLOR]) = f(c.Value)
        [COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Perfectly done. I am very grateful for the time and energy you put into this for me. :bow::bow::bow:

So that I don't come back wakeing this post again one day, please lecture me on these two lines

1.
Code:
[FONT='inherit'] [COLOR=Royalblue]Call[/COLOR] toRank(qr, m, d, [COLOR=Brown]4[/COLOR], [COLOR=Brown]13[/COLOR], ary)
[/FONT]

2.
Code:
[FONT='inherit'][COLOR=Royalblue]Call[/COLOR] toRank(qr, m, d, [COLOR=Brown]14[/COLOR], [COLOR=Brown]14[/COLOR], ary)
[/FONT]

What are they doing actually?

Have a wonderful time
 
Upvote 0
One more issue, I tested the code with all cells being filled. Then I just tried it on data with some blanks and this line was not able to be processed.

Code:
[FONT='inherit']  arz(i) = WorksheetFunction.Large(arb, i)
 [/FONT]

How do I skip the blanks?
 
Upvote 0
How do I skip the blanks?
Use this:

Code:
[FONT=Lucida Console][COLOR=Royalblue]Sub[/COLOR] Kelly()
[COLOR=Royalblue]Dim[/COLOR] d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] m [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], qr [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], qq [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] ary


Application.ScreenUpdating = False
qq = Range([COLOR=Darkcyan]"D"[/COLOR] & Rows.Count).[COLOR=Royalblue]End[/COLOR](xlUp).Row
Range([COLOR=Darkcyan]"P"[/COLOR] & [COLOR=Brown]7[/COLOR] & [COLOR=Darkcyan]":Y"[/COLOR] & qq).ClearContents
[COLOR=Royalblue]For[/COLOR] qr = [COLOR=Brown]7[/COLOR] [COLOR=Royalblue]To[/COLOR] qq
    
    m = WorksheetFunction.CountIf(Range([COLOR=Darkcyan]"C7:C"[/COLOR] & qq), Range([COLOR=Darkcyan]"C"[/COLOR] & qr))  'case insensitive
        
        ary = Split([COLOR=Darkcyan]"[COLOR=Brown]100[/COLOR] [COLOR=Brown]95[/COLOR] [COLOR=Brown]90[/COLOR]"[/COLOR])
        ary = Application.Transpose(Application.Transpose(ary))
        [I][COLOR=Dimgray]'ary = Application.Transpose(Range("O2", Cells(Rows.Count, "O").End(xlUp)))[/COLOR][/I]
        
        [COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
            [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](ary)
                d([COLOR=Royalblue]CLng[/COLOR](ary(i))) = [COLOR=Royalblue]Empty[/COLOR]
            [COLOR=Royalblue]Next[/COLOR]
        [COLOR=Royalblue]Call[/COLOR] toRank(qr, m, d, [COLOR=Brown]4[/COLOR], [COLOR=Brown]13[/COLOR], ary)
        
        
        ary = Split([COLOR=Darkcyan]"[COLOR=Brown]1000[/COLOR] [COLOR=Brown]950[/COLOR] [COLOR=Brown]900[/COLOR]"[/COLOR])
        ary = Application.Transpose(Application.Transpose(ary))
        [I][COLOR=Dimgray]'ary = Application.Transpose(Range("AA2", Cells(Rows.Count, "AA").End(xlUp)))[/COLOR][/I]
        
        [COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
            [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](ary)
                d([COLOR=Royalblue]CLng[/COLOR](ary(i))) = [COLOR=Royalblue]Empty[/COLOR]
            [COLOR=Royalblue]Next[/COLOR]
        [COLOR=Royalblue]Call[/COLOR] toRank(qr, m, d, [COLOR=Brown]14[/COLOR], [COLOR=Brown]14[/COLOR], ary)

    qr = qr + m - [COLOR=Brown]1[/COLOR]

[COLOR=Royalblue]Next[/COLOR] qr

Application.ScreenUpdating = True

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR]

[COLOR=Royalblue]Sub[/COLOR] toRank(qr [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], m [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], a [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], b [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], ary [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Variant[/COLOR])
[I][COLOR=Dimgray]'https://www.mrexcel.com/forum/excel-questions/1113655-ranking-code-amendment-system-reserved-numbers-vba.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], z [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], yy [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] e [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], f [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] arz, arb
[COLOR=Royalblue]Dim[/COLOR] c [COLOR=Royalblue]As[/COLOR] Range, r [COLOR=Royalblue]As[/COLOR] Range, ra [COLOR=Royalblue]As[/COLOR] Range


[COLOR=Royalblue]For[/COLOR] g = a [COLOR=Royalblue]To[/COLOR] b

    [COLOR=Royalblue]Set[/COLOR] r = Cells(qr, g).Resize(m)
    yy = WorksheetFunction.CountA(r)
    i = [COLOR=Brown]0[/COLOR]

    [COLOR=Royalblue]If[/COLOR] yy > [COLOR=Brown]0[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]ReDim[/COLOR] arb([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] yy)
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] ra In r
        [COLOR=Royalblue]If[/COLOR] Len(ra) > [COLOR=Brown]0[/COLOR] [COLOR=Royalblue]Then[/COLOR] i = i + [COLOR=Brown]1[/COLOR]: arb(i) = ra
        [COLOR=Royalblue]Next[/COLOR]
    [COLOR=Royalblue]Else[/COLOR]
        [COLOR=Royalblue]GoTo[/COLOR] skip:
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]

    [COLOR=Royalblue]ReDim[/COLOR] arz([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb))
        
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb)
            arz(i) = WorksheetFunction.Large(arb, i)
             
        [COLOR=Royalblue]Next[/COLOR] i
    
    n = arz([COLOR=Royalblue]UBound[/COLOR](arz))
    
    [COLOR=Royalblue]Set[/COLOR] e = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arz)
            e(arz(i)) = [COLOR=Royalblue]Empty[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
    [COLOR=Royalblue]Set[/COLOR] f = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        z = [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]For[/COLOR] i = ary([COLOR=Brown]1[/COLOR]) [COLOR=Royalblue]To[/COLOR] n [COLOR=Royalblue]Step[/COLOR] -[COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]If[/COLOR] d.Exists(i) [COLOR=Royalblue]And[/COLOR] [COLOR=Royalblue]Not[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR]
            z = z + [COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[I][COLOR=Dimgray]'            If e.Exists(i) Then f(i) = z: z = z + 1[/COLOR][/I]
            [COLOR=Royalblue]If[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR] f(i) = z & GetOrdinalSuffixForRank(z): z = z + [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] c In Cells(qr, g).Resize(m)
            [COLOR=Royalblue]If[/COLOR] f.Exists(c.Value) [COLOR=Royalblue]Then[/COLOR] c.Offset(, [COLOR=Brown]12[/COLOR]) = f(c.Value)
        [COLOR=Royalblue]Next[/COLOR]
skip:
[COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
You are very intelligent!!!

Very great.

Another issue I observed is that when I have two same ranks, like 1st, 1st, the next rank is still recorded as 2nd.

Meanwhile, that's the third. Is there a work around for it too?

Regard, Kelly
 
Upvote 0
Another issue I observed is that when I have two same ranks, like 1st, 1st, the next rank is still recorded as 2nd.

Meanwhile, that's the third. Is there a work around for it too?

Try this one:

Code:
[FONT=Lucida Console][COLOR=Royalblue]Sub[/COLOR] toRank(qr [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], m [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], a [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], b [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], ary [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Variant[/COLOR])
[I][COLOR=Dimgray]'https://www.mrexcel.com/forum/excel-questions/1113655-ranking-code-amendment-system-reserved-numbers-vba.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], z [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], yy [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] e [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR], f [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] arz, arb
[COLOR=Royalblue]Dim[/COLOR] c [COLOR=Royalblue]As[/COLOR] Range, r [COLOR=Royalblue]As[/COLOR] Range, ra [COLOR=Royalblue]As[/COLOR] Range


[COLOR=Royalblue]For[/COLOR] g = a [COLOR=Royalblue]To[/COLOR] b

    [COLOR=Royalblue]Set[/COLOR] r = Cells(qr, g).Resize(m)
    yy = WorksheetFunction.CountA(r)
    i = [COLOR=Brown]0[/COLOR]

    [COLOR=Royalblue]If[/COLOR] yy > [COLOR=Brown]0[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]ReDim[/COLOR] arb([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] yy)
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] ra In r
        [COLOR=Royalblue]If[/COLOR] Len(ra) > [COLOR=Brown]0[/COLOR] [COLOR=Royalblue]Then[/COLOR] i = i + [COLOR=Brown]1[/COLOR]: arb(i) = ra
        [COLOR=Royalblue]Next[/COLOR]
    [COLOR=Royalblue]Else[/COLOR]
        [COLOR=Royalblue]GoTo[/COLOR] skip:
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]

    [COLOR=Royalblue]ReDim[/COLOR] arz([COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb))
        
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arb)
            arz(i) = WorksheetFunction.Large(arb, i)
             
        [COLOR=Royalblue]Next[/COLOR] i
    
    n = arz([COLOR=Royalblue]UBound[/COLOR](arz))
    
    [COLOR=Royalblue]Set[/COLOR] e = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        [COLOR=Royalblue]For[/COLOR] i = [COLOR=Brown]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=Royalblue]UBound[/COLOR](arz)
            e(arz(i)) = e(arz(i)) + [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]Next[/COLOR]
    
    [COLOR=Royalblue]Set[/COLOR] f = CreateObject([COLOR=Darkcyan]"scripting.dictionary"[/COLOR])
        z = [COLOR=Brown]1[/COLOR]
        [COLOR=Royalblue]For[/COLOR] i = ary([COLOR=Brown]1[/COLOR]) [COLOR=Royalblue]To[/COLOR] n [COLOR=Royalblue]Step[/COLOR] -[COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]If[/COLOR] d.Exists(i) [COLOR=Royalblue]And[/COLOR] [COLOR=Royalblue]Not[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR]
            z = z + [COLOR=Brown]1[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[I][COLOR=Dimgray]'            If e.Exists(i) Then f(i) = z: z = z + 1[/COLOR][/I]
            [COLOR=Royalblue]If[/COLOR] e.Exists(i) [COLOR=Royalblue]Then[/COLOR] f(i) = z & GetOrdinalSuffixForRank(z): z = z + e(i)
        [COLOR=Royalblue]Next[/COLOR]
    
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] c In Cells(qr, g).Resize(m)
            [COLOR=Royalblue]If[/COLOR] f.Exists(c.Value) [COLOR=Royalblue]Then[/COLOR] c.Offset(, [COLOR=Brown]12[/COLOR]) = f(c.Value)
        [COLOR=Royalblue]Next[/COLOR]
skip:
[COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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