Want to create a RANK FUNCTION in VBA

oleppedersen

New Member
Joined
Mar 23, 2013
Messages
13
Hi, sorry if this is answered somewhere else; if so I was unable to track it down.

I am pretty good with Excel, but have little knowledge of VBA.

I have a massive database where everything is ranked by various criteria.

I would like to create a RANK function in VBA instead, so that the 1.5 million RANK commands in Excel can be dispensed of, hopefully speeding up the process.

I have tried a bit myself, also with some help of "Learn VBA"-sites, to no avail. When I use the Worksheet.Function.Rank syntax listed over at Microsoft, my Excel 2013 (Norwegian version) won't do what I would like.

As soon as I crack the basics, I think I am savvy enough (thanks to learning BASIC in the early 1980-ies) to create a loop.

Lets say my data that needs to be ranked are in range A1:A10, and I need to put the ranking (descending) in C1:C10. What should the code look like?

Here is my abysmal try:

Function Rangering() As Integer
Dim Liste As Range
Set Liste = Range("A1:A10")


WorksheetFunction.Rank(Range("A1"),Liste)


End Function


---
Unfortunately, noone around that I can ask for help where I work :-(
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This should help you:

Code:
Sub rangering()

    Dim liste As Range
    Set liste = Range("A1:A10")
    
    For Each cell_a In liste
        Range(cell_a.Address).Offset(0, 1).Value = WorksheetFunction.Rank(cell_a.Value, liste, 1) ' change 1 to 0 for desceending rank
    Next cell_a

End Sub

This will add the numbers rank one cell to the right of the range of numbers. So, if your range is A1:A10, B1:B10 shows the ranks...

I hope this helps!
 
Upvote 0
This will add the numbers rank one cell to the right of the range of numbers. So, if your range is A1:A10, B1:B10 shows the ranks...

I hope this helps!

Brilliant! Thank you so much - not only does it work, I also understand better than before how the FOR/NEXT have to be used. I owe you one :-)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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