Consulting

Results 1 to 9 of 9

Thread: Ranking the rows without sorting

  1. #1

    Ranking the rows without sorting

    Hi,
    I have a list of participants for a contest. As such, I need to rank the participants according to their respective scores from highest (1) to lowest (500).
    The easiest way is definitely sorting the names by score from highest to lowest but I am not allowed to do that. My superior wants the list to maintain its originality while the names gets ranked. Can anyone suggest some help that uses VBA to code? A sample of the list looks like this:

    Names
    Score
    Rank
    Jason M. 34
    Mark W. 12
    Clark P. 50
    Alex C. 98
    Alexis Q. 93
    Nathaniel A. 23
    Lewis L. 45
    James J. 78
    Joseph G. 54
    Jessie G. 32
    Joe D. 34
    John H. 53
    Pepper M. 99
    Nathalie M. 93

    And the list goes on... Appreciate if anyone could help me with it.
    I wanted VBA because this will not be only time I have to do this. So if there is a code to it, I can just use the code again the next time this task comes by.

  2. #2
    VBAX Regular
    Joined
    Jan 2011
    Posts
    35
    Location
    Try this:-
    Sub RK()
    Dim Rng As Range, Dn As Range, n As Long, c As Long
    Set Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
    ReDim ray(1 To Application.Max(Rng))
    For Each Dn In Rng
        ray(Dn.Value) = ray(Dn.Value) & IIf(ray(Dn.Value) = "", Dn.Address, "," & Dn.Address)
    Next Dn
    For n = UBound(ray) To 1 Step -1
        If ray(n) <> "" Then
            c = c + 1
            For Each Dn In Range(ray(n)).Areas
                Range(Dn.Address).Offset(, 1).Value = c
            Next Dn
        End If
    Next n
    End Sub

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    By formula is simpler
    =RANK(B2,$B$2:$B$501,0)

    Using VBA
        Selection.Formula = "=RANK(" & Selection(1).Offset(, -1).Address(0, 0) & "," & Selection.Offset(, -1).Address(1, 1) & ",0)"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Add helper column. Fill series to bottom with numbers 1 to?

    Sort by Score. Fill Rank column with series to bottom with numbers 1 to?

    Sort by helper column, then delete helper column.

    Coulda done it faster then typing this response. But I'm a slow typist
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This will show equal places as "2=" etc.
    Option Explicit
    Sub Ranks()
        Dim r As Range, c As Range
        Dim s As Long, i As Long, k As Long
        Dim FA As String
    
    
        Set r = Cells.Find("Score")
        Set r = Range(r(2), r(2).End(xlDown))
        s = Application.Max(r)
        i = 1
        For s = Application.Max(r) To 1 Step -1
            k = 0
            With r
                Set c = .Find(s, lookat:=xlWhole)
                If Not c Is Nothing Then
                    FA = c.Address
                    Do
                        k = k + 1
                        Set c = .FindNext(c)
                    Loop Until c.Address = FA
                    
                    Set c = .Find(s, lookat:=xlWhole)
                    FA = c.Address
                    Do
                        If k > 1 Then
                            c.Offset(, 1) = i & "="
                        Else
                            c.Offset(, 1) = i
                        End If
                        Set c = .FindNext(c)
                    Loop Until c.Address = FA
                End If
            End With
            i = i + k
        Next s
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i use below in one of my projects...

    Sub vbax_58943_rank_range()
        
        Dim rng As Range
        
        With Worksheets("Sheet1")
            Set rng = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
            rng.Offset(, 1).Value = Application.Rank(rng, rng)
        End With
    End Sub
    Application.Rank(rng, rng) or Application.Rank(rng, rng, 0) ranks in descending order

    Application.Rank(rng, rng, 1) ranks in ascending order (third argument, the order, any nonzero value as per MS help)
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub vbax_58943_rank_range()
    I like that idea. I keep forgetting where to find what I was previously working on.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i don't remember where i first saw this, "naming the sub with the thread number". mr excel or excel forum.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    Thanks guys. The suggestions worked.
    It helped a bunch on my work now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •