How to sort data with a UDF function

afndst

Board Regular
Joined
Sep 8, 2015
Messages
59
Office Version
  1. 365
Platform
  1. Windows
´Hello
I am developing UDF funtions to compute the percentiles using the R methods.
For that purpose I need to sort the data within the function, sorting the data for another variable with no interaction with the spreadsheet.
The searching results I have been getting use the sort function of Excel to return the sorted data in a new range of the spreadsheet.
How can I achieve my intention?

Regards

António Teixeira









RTegard
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi @afndst. I hope you're well.

Check out these examples to sort arrays in memory, without using cell ranges.


:cool:
 
Upvote 0
Hello

It can be done using the worksheet SORT function with one column data sets. The code shown below transforms all the ranges submited into that format.

Function xlSort_wsf(x As Range, Optional ord = 1)

'ord = 1: Ascending
'ord = -1: Descending

Dim MyData() As Variant
Dim i As Long, n As Long
n = x.Cells.Count
ReDim MyData(1 To n, 1 To 1) ' Vector column

For i = 1 To n
MyData(i, 1) = x(i)
Next i

xlSort_wsf = WorksheetFunction.Sort(MyData, , ord)

End Function

RQuartis_018.xlsm
ABCDEFG
1
21201-1
3-110-3100
4-320-120
5310120
64100110
751310
845
9=xlSort_wsf($B$2:$C$7,E2)54
10=xlSort_wsf($B$2:$C$7,F2)103
11101
12201
1320-1
14100-3
15
AUX01
Cell Formulas
RangeFormula
E3:F14E3=xlSort_wsf($B$2:$C$7,E2)
B9B9=FORMULATEXT(E3)
B10B10=FORMULATEXT(F3)
Dynamic array formulas.
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

If you have 365 then you could use a formula
Fluff.xlsm
ABCDEF
1
21201-1
3-110-3100
4-320-120
5310120
64100110
751310
845
954
10103
11101
12201
1320-1
14100-3
Main
Cell Formulas
RangeFormula
E3:E14E3=SORT(TOCOL(B2:C7),,E2)
F3:F14F3=SORT(TOCOL(B2:C7),,F2)
Dynamic array formulas.
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

If you have 365 then you could use a formula
Fluff.xlsm
ABCDEF
1
21201-1
3-110-3100
4-320-120
5310120
64100110
751310
845
954
10103
11101
12201
1320-1
14100-3
Main
Cell Formulas
RangeFormula
E3:E14E3=SORT(TOCOL(B2:C7),,E2)
F3:F14F3=SORT(TOCOL(B2:C7),,F2)
Dynamic array formulas.
I was just in the process of typing this along with a UDF if the OP for some reason still wanted that. You posted the formula, so no need for me to do that, but here is the UDF...
VBA Code:
Function xlSort(Rng As Range, Optional Ord As Long = 1) As Variant
  xlSort = Evaluate("SORT(TOCOL(" & Rng.Address & "),," & Ord & ")")
End Function
 
Upvote 0
Solution
I was just in the process of typing this along with a UDF if the OP for some reason still wanted that. You posted the formula, so no need for me to do that, but here is the UDF...
VBA Code:
Function xlSort(Rng As Range, Optional Ord As Long = 1) As Variant
  xlSort = Evaluate("SORT(TOCOL(" & Rng.Address & "),," & Ord & ")")
End Function
Hello

It's perfect.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,109
Messages
6,123,136
Members
449,098
Latest member
Doanvanhieu

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