UnsolvedCoding
Technical User
I searched all over to find a simple way to sort an array in VBA. This sub works but keep in mind that this array is determined by having your selection highlighted before running the sub and will put the sorted information back into the portion you selected at the end of the sort.
If you want to do a different array you will need to set up your redim preserve statments since they are not included.
Sub Sort_Array()
Dim sortingArray As Variant
Dim i As Long
Dim j As Long
Dim temp As Variant
' Create Array
sortingArray = Selection.Value
For i = 1 To (UBound(sortingArray, 1) - 1)
For j = i To UBound(sortingArray, 1)
If Val(sortingArray(j, 1)) < Val(sortingArray(i, 1)) Then
temp = sortingArray(i, 1)
sortingArray(i, 1) = sortingArray(j, 1)
sortingArray(j, 1) = temp
End If
Next j
Next i
' Place array
Selection.Value = sortingArray
End Sub
If you want to do a different array you will need to set up your redim preserve statments since they are not included.
Sub Sort_Array()
Dim sortingArray As Variant
Dim i As Long
Dim j As Long
Dim temp As Variant
' Create Array
sortingArray = Selection.Value
For i = 1 To (UBound(sortingArray, 1) - 1)
For j = i To UBound(sortingArray, 1)
If Val(sortingArray(j, 1)) < Val(sortingArray(i, 1)) Then
temp = sortingArray(i, 1)
sortingArray(i, 1) = sortingArray(j, 1)
sortingArray(j, 1) = temp
End If
Next j
Next i
' Place array
Selection.Value = sortingArray
End Sub