Use VBA Function to calculate the rank

ddddd123

New Member
Joined
Mar 11, 2022
Messages
24
Office Version
  1. 2021
Platform
  1. Windows
I want to create a VBA function to calculate the rank, like that
VBA Code:
Function CALCULATERANKING(score As Double, list As Range) As Integer
    Dim row As Integer
    Dim column As Integer

    CalculateRanking = 1

    For row = 1 To list.Rows.count
        If list.Cells(row, 1).Value > score Then
            CalculateRanking = CalculateRanking + 1
        Else
            Exit For
        End If
    Next
End Function

But this can't meet my requirements. If I use CALCULATERANKING to calculate the table below it doesn't work. Should I use Lookup or something else to fix it?
Student​
Score​
Rank​
Jason981
Peter604
Johnny703
Amy285
Zoe852
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Why do you need VBA for this?

Use the formula =RANK()

1648538924784.png


If you still want VBA then the below can solve your problem.

VBA Code:
Range("C2:C6").Formula = "=RANK(B2,$B$2:$B$6)"
 
Upvote 0
Can create a new function in a VBA module to acquire the ranking of a student? I understand this would be more troublesome, but I would like to know how to fix it.

But still thank for your answer!
 
Upvote 0
Can create a new function in a VBA module to acquire the ranking of a student? I understand this would be more troublesome, but I would like to know how to fix it.

But still thank for your answer!

You mean like this?

VBA Code:
Option Explicit

Function CALCULATERANKING(score As Double, list As Range) As Integer
    CALCULATERANKING = Application.WorksheetFunction.Rank(score, list)
End Function

1648544105382.png
 
Upvote 0
Solution

Forum statistics

Threads
1,215,523
Messages
6,125,319
Members
449,218
Latest member
Excel Master

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