excel vba application.sort code example

Example 1: vba sort the data Z to A

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

Example 2: vba sort the data Z to A

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

Tags:

Vb Example