VBA sort multiple columns and multiple keys array

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
158
I'm looking for a function that can sort multiple columns and multiple keys array, and it should be dynamic even on even at the expense of performance.

This function should have 3 parameters:

  1. The 2 dimensions array we want to sort.
  2. An array of the "columns key" numbers we want to sort.
  3. An boolean array that True = Ascending and False = Descending
In the end this function should return the sorted array.

For example:

Dim SortedArray as Variant

SortedArray = MySuperSortArrayFnc(MyArr, Array(4,1,7), Array(True, False, False))

MyArr = My 2 dimensions source array that i want to sort.

Array(4,1,7) = the order/levels of the sorting.

Array(True, False, False) = it should be parallel to the previous array parameter, so column 4 is ascending, column 1 is descending and column 7 is descending.

Notes:

  1. The function cant allow to use a worksheet, it should be pure VBA logic.
  2. No need to handle a single array column scenario, only multiple columns array.
  3. No need to handle validation on the input(parameters) like if the "MyArr" paramter is not empty.
  4. Empty values or any other value should be sort order like the excel sort order, if the excel send the empty value to bottom, its fine by me.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Watch MrExcel Video

Forum statistics

Threads
1,130,118
Messages
5,640,215
Members
417,131
Latest member
Seanr19871

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
Top