Order Data UDF

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I wanted help in modifying a RANK UDF to output ORDER values instead of RANK values as per the attached image. Thereby, instead of ranks 2,3,1 for data 5,6,4, the UDF should output the order 3,1,2.

The custom Rank UDF is:
VBA Code:
Public Function Rankvalue( _
       ByVal u As Variant, _
       ByRef v As Variant, _
       Optional ByVal w As Boolean = True) As Variant

    Dim x As Variant, y As Variant

    On Error GoTo ErrHlr

    If VBA.TypeName(v) = "Range" Then
        v = v.Value2
    Else
    End If

    With CreateObject("System.Collections.ArrayList")

        For Each x In v
            If VBA.VarType(x) >= 2& And VBA.VarType(x) <= 6& Then
                .Add VBA.Val(x)
            Else
                GoTo ErrHlr
            End If
        Next x

        .Sort    ' Ascending Order

        If w = False Then    ' Descending Order
            .Reverse    ' Ascending Order
        Else
        End If

        y = Application.WorksheetFunction.Match(u, .ToArray, 0&)
        Rankvalue = y
        Exit Function

    End With

ErrHlr:
    Rankvalue = "?"
    Exit Function

End Function

Order Data vs. Sort Data.jpeg
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I looks like MATCH would do what you want

If you have 5,6,4 in A1:C1
Then MATCH(5, A1:C1, 0) will return 1, MATCH(6, A1:C1, 0) will return 2 etc
 
Upvote 0
I don't understand the logic with how 5,6,4 should result in 3,1,2.

The graphic at the bottom of post 1 shows, the result 1,2,3.
 
Upvote 0
@mikerickson; ... 1,2,3 is the position. If I rank the data 5,6,4 in ascending order I get the ranks as 2,3,1. If this data 5,6,4 was to be ordered the graph is giving the output of 3,1,2 under the last column titled order. It is the actual order of individual values in the master data (5=1, 6=2, 4=3) and shown against the sorted data. 4,5,6 gives (3,1,2) as 4 is at 3rd position, 5 is at first position and 6 is at third position in unsorted data.
 
Last edited:
Upvote 0
To find the position in the original order, use that array in that order (cells on worksheet?) as the second argument of MATCH.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
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