Sorting question

Mr930

Well-known Member
Joined
Aug 31, 2006
Messages
585
I have 15 columns of numbers, they will always be the same length. I need to sort each one independent of the others. The columns are side by side all the time, and are calculated in an existing function. I would like to do this in VBA rather than doing an sort from the menu. I would like to create a function or subroutine that can take a passed in range and give back the sorted range. I am sure there are various ways to do this, can anyone suggest the best?

thanks
Fred
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
one option

Here is one possible solution. I'm not sure if it's "The Best" but it should work:

Sub Sort_Selection(Optional r As Range)
'Created by 4XL Solutions on 9/8/06
'Get the Address of the selected range
If r Is Nothing Then Set r = ActiveWindow.RangeSelection
'Get the first cell in the selected range
k = Left(r.Address, InStr(1, r.Address, ":") - 1)
' Sort the selected range
r.Sort Key1:=Range(k), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub


This macro will accept a passed range, and sort that passed range. Also, it can be run directly by first selecting the range in Excel manually, then running the macro.

Good luck!
 
Upvote 0
Thanks!

Question, what does the word "optional" mean below? I have never seen that before.

Sub Sort_Selection(Optional r As Range)



Fred
 
Upvote 0
"Optional" means that the macro can accept a passed variable, like you would do with a function, but it is not required. The fact that it is optional means that the macro will still run, even if a variable isn't passed.

In the latter case, the macro will sort the range that is currently selected in the active worksheet:

If r Is Nothing Then Set r = ActiveWindow.RangeSelection
 
Upvote 0

Forum statistics

Threads
1,225,070
Messages
6,182,672
Members
453,131
Latest member
BeLocke

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