VBA Sort Data Ascending Order Excel Example Macro Code

Home/VBA/VBA Sort Data Ascending Order Excel Example Macro Code

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

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


VBA code to sort data in Ascending Order


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

VBA Sort Data in Ascending Order: Syntax


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


Range.Sort Key1:=Range("A1"), Order1:=xlAscending

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 we can specify the sort order using Order1.

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, Order1:=xlAscending

Sort Data in Ascending using VBA : Examples


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

Sub sb_VBA_Sort_Data_Ascending()
Range("A1:D10").Sort _
Key1:=Range("A1"), Order1:=xlAscending
End Sub

Instructions to run the VBA code to sort data in Excel Workbook in Ascending Order


Please follow the below instructions to execute the VBA code to sort the excel file.
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 the Data in Excel sheet is sorted in Ascending order based on the Column A.

Explained VBA Code to Sort the Excel Data in Ascending order :

‘Starting the program and sub Procedure to write VBA code to sort data in ascending order
Sub sbSortData_Ascending _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”) _
Order1:=xlAscending
‘End ing the sub procedure to sort the data in ascending order
End Sub

VBA to Sort the data in ascending 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 in ascending order. 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, Order1:=xlAscending
End Sub 
LIMITED TIME OFFER
By |April 22nd, 2014|VBA|2 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

2 Comments

  1. manideep May 29, 2017 at 12:11 AM - Reply

    how we can sort data in different sheets of workbook using above function?

  2. swarna June 8, 2017 at 7:44 PM - Reply

    I want to generate a code in visual basics for
    A duty roster.
    By checking all these below conditions
    I have 46 employees
    Only 1 person will do the duty for 1 day.
    Remaining will do the duty in next month.
    Based on their availability we will give the duty. What is the code for that????

Leave A Comment


Related pages


excel offset formulavba msg boxvba coding tutorialvba cell rowclient tracking excel templateformula for adding columns in excelvba choice boxvba clear sheetinputbox excel vbasendmail vbasql faqs with answersexcel unhide all sheetsxml convert to excelexcel duplicate deletehow to delete duplicate records in excelsample project timelineslearn vlookup in excelunprotect excel worksheet without passwordcheck duplicates in excelexcel vba delete rowshow to check if there are duplicates in excelhow to hide unused cells in excel 2010vba tricksexcel vba data entry formiserror vlookup excelrename files using excelexcel vba listboxhow does the vlookup function workcommand to merge cells in excelcopy excel sheet with formulas to another workbookms excel macros examplesprotecting a worksheetinsert macro in excelvba macroddl dml dcl tcl commands in sql with examplesuserform closeunlock password protected excel workbookremove duplicate rows in excelvba copy row from one sheet to anothermsofiledialogfolderpicker vbasql ddl dml dcl tcl commands with examplesmicrosoft excel offset functionproject progress report template excelexcel concatenate sheetsvba connection stringvbscript filesystemobject methodsexcel vba populate comboboxproject assessment template excelsample vba excel codevba copy worksheet to new workbookvb color indexexcel test for nullworking with arrays in excel vbashortkey excelsql basics commandssql questions and answers for written testvba write formula to cellvba excel comboboxworksheet tab excelsmart goals excel templateexcel activate vbapaste special values macromerge worksheets into oneinteractive charts in excel 2010vlookup formula with if conditionlearn to write macros in excelexcel sort vbavba while loopsexcel msgboxinternetexplorer.application vbaexcel remove blank rowsvba object propertiesvba vbcritical