VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

VBA code to sort data with headers in Excel example will help us to sort data in excel worksheets in Ascending or Descending order. We can use Sort method of Excel Range to sort the data. In this example we will see how to sort data in Excel Workbooks worksheets using VBA. VBA code for sorting data in excel Worksheets macro should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.

VBA Sort Data with headers Excel Example Macro Code

VBA code to sort data with headers in excel file

Here is the Example VBA syntax and Example VBA Macro code to sort the Excel Data. This will help you to know how to sort data in Excel worksheets using VBA.

VBA Sort Data with headers in Excel Workbook: Syntax

Following is the VBA Syntax and sample VBA code to Sort the Data in Excel Workbook using VBA. We are using the Sort method of the Excel Workbook Range object.

Range.Sort Key1:=Range("A1") , Header:=xlYes

Here you you can set your range into an object or you can directly use Range object like Range(“A1:D100”). And Key1 will be your Sorting Column which you wants to sort by. And Header is to tell excel that your data is having header row.

Here is VBA code to sort the data in Excel by setting the range to an object:

Dim strDataRange As Range
Dim keyRange As Range
Set strDataRange = Range("Your Data Range")
Set keyRange = Range("Your Sort by Column")
strDataRange.Sort Key1:=keyRange, Header:=xlYes

Sort Data with header row in Excel using VBA: Examples

The following VBA code is to sort the data with headers in Excel Worksheet. This code will sort the data in Range A1 to D10 based on the First Column i.e.; A1.

Sub sb_VBA_Sort_Data()
Range("A1:D10").Sort _
Key1:=Range("A1"), Header:=xlYes
End Sub

Instructions to run the VBA code to sort the data with headers in Excel Workbook

Please follow the below instructions to execute the VBA code to sort the data with headers in excel workbook.
Step 1: Open any existing Excel workbook
Step 2: Enter some data in A1 to D10
Step 3: Press Alt+F11 – This will open the VBA Editor
Step 4: Insert a code module from then insert menu
Step 5: Copy the above code to sort the data in excel and paste in the code module which have inserted in the above step
Step 5: Now press F5 to execute the code

Now you can observe that your Data in Excel sheet is sorted based on the Column A.

Explained VBA Code to sort the Excel Data

Starting the program and sub Procedure to write VBA code to sort data with headers in excel.

Sub sbSortData_VBA_C()
‘Here Range(“A1:D10”) is target range to sort
‘And Range(“A1”) is the sort key to Sort by
Range(“A1:D10”).Sort _
Key1:=Range(“A1”), Header:=xlYes
End Sub
Ending the sub procedure to sort the data with header row.

VBA to Sort the data with header row by assigning to an Object: Examples

It is best practice to assign our target range and key Cell to temporary range objects and then sort the data. Here is the simple example to sort the data in Excel using Objects in VBA.

    Sub sbSortDataInExcel()
    'Delcaring the strDataRange as range store the target range to sort
        Dim strDataRange As Range
    'Delcaring the keyRange as range store the Sort key range to sort by
        Dim keyRange As Range
    'Assigning the target sort Range to strDataRange
        Set strDataRange = Range("A1:D10")
    'Assigning the sort key Range to keyRange
        Set keyRange = Range("A1")
    'Sorting the data using range objects and Sort method
        strDataRange.Sort Key1:=keyRange , Header:=xlYes        
    End Sub 

Excel VBA to sort data in Descending Order

Here is the example macro to sort a range of data in Descending order. This method will have more properties to customize the sort options.

Sub sbSortDataInExcelInDescendingOrder()
 Dim strDataRange, strkeyRange As String
 strDataRange = "C1:F6"
 strkeyRange = "D2:D6"
 With Sheets("Sheet1").Sort
    .SortFields.Add _
        Key:=Range(strkeyRange), _
        SortOn:=xlSortOnValues, _
        Order:=xlDescending, _
     .SetRange Range(strDataRange)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
End With
End Sub

Excel VBA to sort data in Ascending Order

Here is the example macro to sort a range of data in Ascending Order. This method will have more properties to customize the sort options.

Sub sbSortDataInExcelInAscendingOrder()
 Dim strDataRange, strkeyRange As String
 strDataRange = "C1:F6"
 strkeyRange = "D2:D6"
 With Sheets("Sheet1").Sort
    .SortFields.Add _
        Key:=Range(strkeyRange), _
        SortOn:=xlSortOnValues, _
        Order:=xlAscending, _
     .SetRange Range(strDataRange)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
End With
End Sub
Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBATags: Last Updated: June 17, 2022


  1. akshay March 10, 2015 at 9:45 AM

    Sorting in excel with the help of VBA script is amazing. Thanks for the article.

  2. PNRao March 21, 2015 at 2:26 PM

    Thanks Akshay – we are glad to hear such kind of great feedback from our readers. – Thanks-PNRao!

  3. Don Leonard April 21, 2015 at 1:00 AM

    I have been trying to use excels sort method. Thanks for the code. It work for sorting based on one column.
    I would like to sort using several columns that the excel method permits. Everything I have tried does not work. Any suggestions?

  4. Sohail May 14, 2015 at 9:48 AM

    Thanks… I need practical example or situation build up for an event etc.. that more fantasy…


  5. R May 14, 2015 at 10:48 PM

    Haven’t tried this, but I suspect that Key2:=…, Key3:=,,,, etc, is what’ll do the trick

  6. VIvek Sharma May 28, 2015 at 11:39 PM

    What’s wrong in here:

    Application.Workbooks(“bookname.xlsx”).Worksheets(1).Range(“A2:U” & lastrow).Sort Key1:=Range(“F2:F” & lastrow), _
    Order1:=xlAscending, Header:=xlNo

  7. Zel August 13, 2015 at 10:25 PM

    Hi, I am blonde but this sorts descending. How do a sort ascending?

  8. PNRao August 13, 2015 at 11:40 PM

    Hi Zel,

    I have updated the post with the examples. Please check the last example to sort data in ascending order.


  9. Tim October 7, 2015 at 6:43 AM

    Hi PNRao,

    I’m trying to work out a seemingly easy task and, if I had the time and dedication to really get to it, I would probably be able to solve it myself.

    However, due to time constraints and lack of thorough VBA and/or Excel knowledge, I am stuck with the following:

    An excel spread sheet (sheet1) with 1 column (C in my case) with data. Row 1 contains a header, row 2 as well. Row 3 through 655 contain data with the following format:

    1.25L Soft Drinks – 7 Up
    1.25L Soft Drinks – Coke
    1.25L Soft Drinks# – 7 Up
    1.25L Soft Drinks# – Coke

    Basically all the data in column C contains a duplicate, being the only difference the # tag. I would very much like to sort column C in such a way that the products w/o # appear on tab, alphabetically sorted and the products with # appear at the bottom, also alphabetically sorted. A nice to have would be an empty row between the set of data with # and set of data w/o #.

    I am pretty sure that similar codes or other methods have been worked out before but after 3 pretty annoying and time-wasting hours searching the internet my boss really doesn’t want me to get on with it :( I do however HAVE to sort the data. I would have to this multiple times so I am eagerly looking for VBA code.

    Would you have any suggestions on how to code this in VBA?

    Kind regards,


  10. Arun January 19, 2016 at 9:21 PM

    Hi i want to sort date in excel by using VBA .. from list of dates i want to select yesterday date data only.. could u please help

  11. Ambrogio July 20, 2016 at 6:30 AM

    The article on SORTING in Excel with VBA is EXCELLENT, CONCISE & CLEAR . Thank you very much.

  12. Mayura October 18, 2016 at 6:38 AM

    Dear team
    I want to sort my data in macro
    But sometimes data records are 500 and sometimes it is more or less than that then what type of condition i have to give to sort and move the data other sheet

  13. PNRao October 23, 2016 at 9:32 AM

    Find the Dynamic Row/ Last Row before sorting the data and set the sort range accordingly. Please refer the below article to find the last row with data:

    Sub sb_VBA_Sort_DynamicData()
    Dim lRow As Long
    lRow = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
    Do While Application.CountA(ActiveSheet.Rows(lRow)) = 0 And lRow <> 1
        lRow = lRow - 1
    Range("A1:D" &lRow).Sort _
    Key1:=Range("A1"), Header:=xlYes
    End Sub


  14. shyam August 3, 2017 at 10:26 AM

    Hi plz solve this.There is a sequence of numbers in one row


    the code must identify the numbers which are in ascending sequence and print it in the next sheet called Sequence 1.

Leave A Comment