Types of sorting in Excel
There are 2 types of sorting available in Excel. They are Simple and Custom Sorting.
Simple Sorting
Simple sorting is nothing but sorting alphabetically or by number in ascending or descending order.
Sub Sorting()
'--- First Select the Range which you want to sort
Range("I8:L15").Select
'--- Now clear the Sort fields before sorting. This is important otherwise sorting will not take place
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I8"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
'--- Select the Range on which you want to perform the Sort.
'--- If it includes the header row as well then put .Header=xlYes, else xlNo.
.SetRange Range("I8:L15")
.Header = xlYes
'--- It is better to put MatchCase as False. In case you want Sorting as Case senstavie then you can make it "True"
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Custom Sorting
In custom sorting, we do the sorting on the basis of a list of Values. For example: sorting by Month’s or Day’s Name if we do the simple sorting then, all the Months will be sorted alphabetically and so the order will no longer be valid. For such sorting, we create a custom list in an Ascending or Descending Order, how you want.
Note: CustomOrder can have any number of Values in any Order. Sorting will be done exactly in the same order.
For Example:
- For Month Sorting:
- For Some Custom Sorting:
CustomOrder:= "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
CustomOrder:= "Passed,Failed,Inquiry,Defect"
Sub Sorting()
'--- First Select the Range which you want to sort
Range("I8:L15").Select
'--- Now clear the Sort fields before sorting. This is important otherwise sorting will not take place
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
'--- For custom Sorting we need to add the custom list in same order how you want to sort.
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I8"), SortOn:=xlSortOnValues, _
Order:=xlAscending, CustomOrder:= "Mon,Tue,Wed,Thu,Fri,Sat,Sun", DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
'--- Select the Range on which you want to perform the Sort.
'--- If it includes the header row as well then put .Header=xlYes, else xlNo
.SetRange Range("I8:L15")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Hi Sir,
I am ardent follower of you website and its highly appreciated that you have created a blog to resolved the excel related queries. Thanks a lot once again for being so thoughtful.
I have a query, please go through the same and let me know if we have a solution for the same.
Suppose we have large number of data in an excel and I color code those cells or say rows which are not correct or which I do not want for reporting but still want to keep the same for future reference purpurse. In such a case, if I want to filter all the valid data , i.e. all the data that are not color coded then can we do so. Since as long as I have searched in the excel, we cannot filter data based on color code.
Please let me know if this can be done.
Hi Priya,
Thank you for appreciating.
If you are using MS Excel 2007/2010, then it is easy to filter by Cell Color. Just by selecting option from Excel 2007/2010, you can filter it.
Read this Post : http://www.learnexcelmacro.com/2011/11/filter-by-…
But if you are using MS Excel 2003 then for doing so, you need help of VBA/Macro. If you need VBA code for Excel 2003, It will be emailed you.
Was this information useful to you?? Do give your valuable feedback.
Thanks,
Hi!
It's possible to adapt this macro to automaticly sort a table when a new value is included or some date is changed?