VBA Code to Sort Data

Complete Excel VBA Course

HERE IS A ONE LINE CODE FOR DEVELOPER’S REFERENCE WHICH CAN BE USED TO SORT DATA

'Sort data in ascending order on Column F (Created At)
Sheet1.Range("A1:G" & Sheet1.Cells.SpecialCells(xlCellTypeLastCell).Row).Sort Key1:=Sheet1.Range("F1"), Order1:=xlAscending, Header:=xlYes, DataOption1:=xlSortNormal

Before Sort:

VBA Code to sort data

After Sort:

Complete Excel VBA Course
VBA Code to Sort Data

IF YOU ARE LOOKING FOR A VBA CODE THAT CAN SORT THE DATA ON MULTIPLE COLUMNS AT THE SAME TIME THEN BELOW CODE MAY HELP YOU:

Public Sub SortByMultipleColumn()
    '
    'Clear old sort field
    Sheet1.Sort.SortFields.Clear
    'Add sort field on column D (Department ID)
    Sheet1.Sort.SortFields.Add Key:=Sheet1.Range("D2:D20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    'Add sort field on column F (Created At)
    Sheet1.Sort.SortFields.Add Key:=Sheet1.Range("F2:F20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Sheet1.Sort
        .SetRange Range("A1:G20")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    '
End Sub

Before Sort:

After Sort:

Download Practice File

You can also practice this through our practice files. Click on the below link to download the practice file.

Recommended Articles

Excel VBA Course : Beginners to Advanced

We are offering Excel VBA Course for Beginners to Experts at discounted prices. The courses includes On Demand Videos, Practice Assignments, Q&A Support from our Experts. Also after successfully completion of the certification, will share the success with Certificate of Completion

This course is going to help you to excel your skills in Excel VBA with our real time case studies.

Lets get connected and start learning now. Click here to Enroll.

Secrets of Excel Data Visualization: Beginners to Advanced Course

Here is another best rated Excel Charts and Graph Course from ExcelSirJi. This courses also includes On Demand Videos, Practice Assignments, Q&A Support from our Experts.

This Course will enable you to become Excel Data Visualization Expert as it consists many charts preparation method which you will not find over the internet.

So Enroll now to become expert in Excel Data Visualization. Click here to Enroll.

Similar Posts

Leave a Reply

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