VBA Code to Sort Data

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:

35.1 Sort Data

After Sort:

35.2 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
    '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
    End With
End Sub

Before Sort:

35.3 Sort Data

After Sort:

35.4 Sort Data

Leave a Reply

VBA Code to Delete File
VBA Code to Delete File

In this article we are going to show you how you can delete file or files using a single line of VBA code. 1. Delete a specific file from the folder, 2. Delete specific type of files from the folder, 3. Delete all files from the folder

VBA Code to Add Border to Excel Range
VBA Code to Add Border to Excel Range

Through formatting the cells, you can make your Excel data more beautiful and easier to understand. If you record a macro to add borders to Excel range, you will notice that it generates 30 plus lines of code for just a small work. Here we are sharing one line of code which does the same thing.

Protect Excel Sheet for Manual Input
VBA Code to Protect Excel Sheet for Manual Input but Allow Programming Inputs

Did you come across any requirement where you want the user to interact with a sheet only through VBA Form? Here is a simple code which can help you.