Sorting using Excel Macro

.

Sorting in excel sheet, is a very common functionality. Most excel users should be knowing already. But here we are going to learn how to do sorting using Excel VBA. If you are working on any automation in excel, it will be useful to know. You might want to provide users with a sorting button so that they can sort their data simply with a click.

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:

  1. For Month Sorting:
  2. CustomOrder:= "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

  3. For Some Custom Sorting:
  4. 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

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

3 Comments

  1. Priya

    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.

    Reply
  2. admin

    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,

    Reply
  3. Rodrigo

    Hi!

    It's possible to adapt this macro to automaticly sort a table when a new value is included or some date is changed?

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest