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
 
N1N2N3N4N5N6N7N8N9N10TOTALINPUT1RNK1RNK2RNK3RNK4RNK5RNK6RNK7RNK8RNK9RNK10RNKTOTINPUT2
989286807468625650447101002nd3rd4th4th4th4th4th4th4th4th4th1000
96908478726660544842690953rd4th5th5th5th5th5th5th5th5th5th950
94806652382450453040519905th5th6th6th6th6th6th6th6th6th6th900
924550453020454025384306th6th7th7th7th7th7th7th7th7th7th
705642281410342510203098th7th8th8th8th8th8th8th8th8th8th

<tbody>
</tbody>

did this work good for you?
 
Last edited:
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
OK, the table is fine:

Your data are sorted descending, so do you still need this?
Is there a way to make the code work without sorting column A at all? Because it may be hard to get all my columns sorted that way from the original code I posted.
 
Last edited:
Upvote 0
OK, the table is fine:

Your data are sorted descending, so do you still need this?

Yes I still need this part.

I sorted just to help me do the manual ranking for the test post.
 
Upvote 0
OK, try this:
1. You need to add SUM formula in column total.
2. In "Function GetOrdinalSuffixForRank" I changed (Rnk As Double) to (Rnk As Long) & and also change the case of the string for number.
3. The input are hard coded in this line:
ary = Split("100 95 90")
and this:
ary = Split("1000 950 900")
4. Run "Sub Kelly"

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]
[COLOR=Royalblue]Dim[/COLOR] ary

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

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(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(m, d, [COLOR=Brown]14[/COLOR], [COLOR=Brown]14[/COLOR], ary)
Application.ScreenUpdating = True

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


[COLOR=Royalblue]Sub[/COLOR] toRank(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([COLOR=Brown]2[/COLOR], 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([COLOR=Brown]2[/COLOR], 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]


[COLOR=Royalblue]Function[/COLOR] GetOrdinalSuffixForRank(Rnk [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]) [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]String[/COLOR]
 [COLOR=Royalblue]Dim[/COLOR] sSuffix$
[COLOR=Royalblue]If[/COLOR] Rnk [COLOR=Royalblue]Mod[/COLOR] [COLOR=Brown]100[/COLOR] >= [COLOR=Brown]11[/COLOR] [COLOR=Royalblue]And[/COLOR] Rnk [COLOR=Royalblue]Mod[/COLOR] [COLOR=Brown]100[/COLOR] <= [COLOR=Brown]20[/COLOR] [COLOR=Royalblue]Then[/COLOR]
    sSuffix = [COLOR=Darkcyan]"th"[/COLOR]
[COLOR=Royalblue]Else[/COLOR]
    [COLOR=Royalblue]Select[/COLOR] [COLOR=Royalblue]Case[/COLOR] (Rnk [COLOR=Royalblue]Mod[/COLOR] [COLOR=Brown]10[/COLOR])
        [COLOR=Royalblue]Case[/COLOR] [COLOR=Brown]1[/COLOR]: sSuffix = [COLOR=Darkcyan]"st"[/COLOR]
        [COLOR=Royalblue]Case[/COLOR] [COLOR=Brown]2[/COLOR]: sSuffix = [COLOR=Darkcyan]"nd"[/COLOR]
        [COLOR=Royalblue]Case[/COLOR] [COLOR=Brown]3[/COLOR]: sSuffix = [COLOR=Darkcyan]"rd"[/COLOR]
        [COLOR=Royalblue]Case[/COLOR] [COLOR=Royalblue]Else[/COLOR]: sSuffix = [COLOR=Darkcyan]"th"[/COLOR]
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Select[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
     GetOrdinalSuffixForRank = sSuffix
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Function[/COLOR][/FONT]
 
Upvote 0
When I run it I get runtime error 1004

Unable to get the Large property of the worksheetFunction class

Then when I went to debug, the value for "i" inside arz(i) was 6.

One thing to point out - I am thinking that might be the cause.

My data start from row 7.

The previous rows are all headers.
 
Upvote 0
My data start from row 7.
Change this part:

Code:
m = Range("D" & Rows.Count).End(xlUp).Row - 1    ' change 1 to 6
--
arb = Application.Transpose(Cells(2, g).Resize(m))     ' change 2 to 7
---
For Each c In Cells(2, g).Resize(m)            ' change 2 to 7
 
Upvote 0
Change this part:

Code:
m = Range("D" & Rows.Count).End(xlUp).Row - 1    ' change 1 to 6
--
arb = Application.Transpose(Cells(2, g).Resize(m))     ' change 2 to 7
---
For Each c In Cells(2, g).Resize(m)            ' change 2 to 7


Oh my GOODNESS!!!!

you are too much, @Akuini

Very fast and smooth
Thanks so much, I am very grateful
 
Upvote 0
Hello @Akuini,

There was one thing I forgot to mention during my last requirements:

From the original code I posted, column C was used to uniquely rank the items.

So the data is sorted by column C.

So all X will line up before all Y will follow then all Z come.

So the motive from the original code posted was to rank uniquely for all X , all Y and all Z.

Can this be done?
 
Upvote 0
There's no data in col C in your data example, what are X,Y,Z?
And what do you mean by "rank uniquely for all X , all Y and all Z"?
Can you give me an example?
 
Upvote 0
CDEFGHIJKLMNOPQRSTUVWXYZ
6CAT.N1N2N3N4N5N6N7N8N9N10TOTR1R2R3R4R5R6R7R8R9R10RTOT
7X675486907468625650446515th4th4th3rd5th5th5th5th5th5th4th
8X969045784566605448426242nd3rd5th4th6th6th6th6th6th6th5th
9Y948066523824504530405193rd4th4th4th4th4th4th4th4th4th4th
10Y924550453020454025384304th5th5th5th5th5th5th5th5th5th5th
11Y705642281410342510203096th7th7th7th7th7th7th7th7th7th6th

<tbody>
</tbody>

something like this layout.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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