The SORT function sorts the contents of a range or array dynamically. Perfect for organizing data automatically, creating sorted reports, and maintaining clean data presentation without manual intervention.
Master the fundamentals of Excel SORT function
SORT is a dynamic array function that organizes data automatically based on specified criteria. It provides formula-based sorting that updates automatically when source data changes, eliminating the need for manual sorting operations.
Automatically updates when source data changes
Maintains row integrity when sorting multi-column arrays
Ascending or descending with simple parameter
Does not modify original data
Function-specific parameters
Function-specific return type
Automatically organize large datasets
Create self-sorting reports
Generate sorted reports automatically
Present data in organized format
Exact matching required
Returns numeric position
Handles missing text gracefully
=SORT(array, sort_index, sort_order, by_col)The array or range to sort (required)
Column index to sort by (default is 1)
1 for ascending (default), -1 for descending
FALSE to sort by rows (default), TRUE to sort by columns
Sorted dynamic array
Description: Sorts and returns an array based on specified criteria
Sort values in ascending order
Sorts values from smallest to largest (A-Z)
Sort arrays in VBA using built-in methods
' Sort range in VBA
Sub SortRangeVBA()
Dim ws As Worksheet
Dim sortRange As Range
Set ws = ActiveSheet
Set sortRange = ws.Range("A1:A10")
' Sort ascending
sortRange.Sort Key1:=sortRange.Cells(1), _
Order1:=xlAscending, _
Header:=xlNo
MsgBox "Range sorted ascending"
End Sub
' Advanced sort with multiple columns
Sub SortMultipleColumns()
Dim ws As Worksheet
Dim dataRange As Range
Set ws = ActiveSheet
Set dataRange = ws.Range("A1:C100")
' Sort by column B, then by column C
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=dataRange.Columns(2), _
SortOn:=xlSortOnValues, _
Order:=xlAscending
.SortFields.Add Key:=dataRange.Columns(3), _
SortOn:=xlSortOnValues, _
Order:=xlDescending
.SetRange dataRange
.Header = xlYes
.Apply
End With
MsgBox "Multi-column sort complete"
End SubCreate array sorting function in VBA
' Bubble sort implementation for small arrays
Function SortArray(inputArray As Variant, ascending As Boolean) As Variant
Dim outputArray As Variant
Dim i As Long, j As Long
Dim temp As Variant
outputArray = inputArray
' Bubble sort algorithm
For i = LBound(outputArray) To UBound(outputArray) - 1
For j = LBound(outputArray) To UBound(outputArray) - i - 1
If ascending Then
If outputArray(j) > outputArray(j + 1) Then
temp = outputArray(j)
outputArray(j) = outputArray(j + 1)
outputArray(j + 1) = temp
End If
Else
If outputArray(j) < outputArray(j + 1) Then
temp = outputArray(j)
outputArray(j) = outputArray(j + 1)
outputArray(j + 1) = temp
End If
End If
Next j
Next i
SortArray = outputArray
End Function
' Usage
Sub Example_SortArray()
Dim myArray As Variant
Dim sorted As Variant
myArray = Array(5, 2, 8, 1, 9, 3)
sorted = SortArray(myArray, True)
MsgBox "Sorted: " & Join(sorted, ", ")
End SubSort sales data by amount or date
Organize employees by department or name
Sort transactions by amount or date
Sort products by stock level or name
Combine with FILTER to show top records
Not enough space for sorted array
Ensure adequate space for sorted resultsSolution: Clear cells to allow array to spill
Data not sorting as expected
Verify column number and 1/-1 for directionSolution: Check sort_index and sort_order parameters
Text and numbers not sorting correctly
Convert all to same type with VALUE or TEXTSolution: Ensure consistent data types in sort column
Header gets sorted with data
Use =SORT(A2:A100) not A1:A100Solution: Start range below header or use SORTBY