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 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.
‘Here Range(“A1:D10”) is target range to sort
‘And Range(“A1”) is the sort key to Sort by
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.Clear .SortFields.Add _ Key:=Range(strkeyRange), _ SortOn:=xlSortOnValues, _ Order:=xlDescending, _ DataOption:=xlSortNormal .SetRange Range(strDataRange) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply 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.Clear .SortFields.Add _ Key:=Range(strkeyRange), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortNormal .SetRange Range(strDataRange) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub
Sorting in excel with the help of VBA script is amazing. Thanks for the article.
Thanks Akshay – we are glad to hear such kind of great feedback from our readers. – Thanks-PNRao!
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?
Thanks… I need practical example or situation build up for an event etc.. that more fantasy…
Haven’t tried this, but I suspect that Key2:=…, Key3:=,,,, etc, is what’ll do the trick
What’s wrong in here:
Application.Workbooks(“bookname.xlsx”).Worksheets(1).Range(“A2:U” & lastrow).Sort Key1:=Range(“F2:F” & lastrow), _
Hi, I am blonde but this sorts descending. How do a sort ascending?
I have updated the post with the examples. Please check the last example to sort data in ascending order.
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?
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
The article on SORTING in Excel with VBA is EXCELLENT, CONCISE & CLEAR . Thank you very much.
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
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:
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.