
- HOW TO SORT MULTIPLE COLUMNS IN EXCEL BY DATE HOW TO
- HOW TO SORT MULTIPLE COLUMNS IN EXCEL BY DATE CODE
MyArray = Application.Transpose(ActiveSheet.Range("D4:D13"))

Let’s sort the employees based on the joining dates.įirst, we’ll convert the joining dates ( D4:D13) to a one-dimensional Array. We can create a sorted copy of the data set in a different location, by iterating through each of the rows. Up till now, we’ve sorted the data set in their original location, using the Sort method of VBA. Sort Multiple Columns of a Range in a Different Location through Iteration with VBA in Excel Sorting Columns in Excel While Keeping Rows Togetherģ.
HOW TO SORT MULTIPLE COLUMNS IN EXCEL BY DATE HOW TO

You’ll find the data set B4:D13 in your worksheet sorted first based on the descending order of the salaries, then on the ascending order of the joining dates.
HOW TO SORT MULTIPLE COLUMNS IN EXCEL BY DATE CODE
So the line of code will be: ActiveSheet.Range("B4:D13").Sort Key1:=Range("C8"), Order1:=xlDescending, Key2:=Range("D8"), Order2:=xlAscending The Key1 argument will take a cell reference of the column Salary (Let it be C8), and the Key2 argument will take a cell reference of the column Joining Date (Let it be D8).Īnd Order1 will take xlDescending and Order2 will take xlAscending. We’ll again use the Sort method of VBA, but this time we’ll use the Key argument and the Order argument twice. Let’s try to sort the employees first based on the descending order of their salaries, then on the ascending order of their joining dates. Now, we’ll sort multiple columns of a range based on multiple columns. Sort Multiple Columns of a Range based on Multiple Columns with VBA in Excel Read More: How to Sort Range Using VBA in Excel (6 Examples)Ģ. You’ll find the data set B4:D13 in your worksheet sorted according to the ascending order of the joining dates of the employees. Therefore, the line of code will be: ActiveSheet.Range("B4:D13").Sort Key1:=Range("D8"), Order1:=xlAscending So the Order1 argument will be xlAscending. Let it be D8.Īnd we want to sort in ascending order. So the Key1 argument needs to be any cell reference within the range D4:D13. Now, we want to sort the data set according to the joining dates ( D4:D13). Here we want to sort based on a single column, so we’ll take only the Key1 and the Order1 argument. The Order argument denotes whether you want to sort in ascending order or descending order. The Key argument takes a cell reference of the column based on which you want to sort your data set. Knowing only the two arguments Key and Order will do. You needn’t know all the arguments mentioned here at this moment.

The syntax of the VBA Sort method is: =Sort (Key1, Order1, Key2, Type, Order2, Ke圓, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)ĭon’t worry. Let’s try to sort the data set in ascending order according to the joining dates of the employees. Sort Multiple Columns of a Range Based on a Single Column with VBA in Excelįirst, we’ll learn to sort multiple columns of a data set based on a single column with the Sort method of VBA. Our objective today is to sort the multiple columns of this data set with Excel VBA.ġ. Here we’ve got a data set with the names, salaries, and joining dates of some employees of a company called Mars Group. 3 Suitable Ways to Sort Multiple Columns with Excel VBA
