you posted a blank workbook with some buttons and recorded / modified range sorting macros.
your assignment is sorting (via separate function) an array which will be populated from and written back to a range.
i will make an exception and provide the code. but it is your responsibility to fully understand the procedures (without my help, for sure) and explain the details to the tutor where necessary.
i used popular bubble sort method you can easily find. my contribution is adding the sort method only.
Option Explicit
Option Base 1
Sub vbax_53555_SortAndRewriteRangeBasedArray()
Dim rng As Range
'Dim MyArr()
Dim MyArr() As Long 'Declare array's data type as integer, long, double, etc if you want to sort numbers
Dim NumArrElements As Long, i As Long, SortOrder As Long
On Error Resume Next
Set rng = Application.InputBox("Select the range to sort", Type:=8)
If rng Is Nothing Then
MsgBox "You pressed Cancel. Please select a range to continue.", vbOKOnly, "Quitting macro..."
Exit Sub
End If
SortOrder = Application.InputBox("Input sort method: 1 for ascending and 2 = Descending", Type:=1)
If SortOrder = 0 Then
MsgBox "You pressed Cancel. Please input 1 or 2.", vbOKOnly, "Quitting macro..."
Exit Sub
End If
On Error GoTo 0
NumArrElements = rng.Count
ReDim MyArr(1 To NumArrElements)
For i = 1 To NumArrElements
MyArr(i) = rng.Cells(i).Value
Next i
BubbleSort MyArr, SortOrder
'Call BubbleSort(MyArr, SortOrder) 'onother way to call the function
rng.Value = Application.Transpose(MyArr) '1D array is like a row. convert it to 'column'.
End Sub
Function BubbleSort(ArrToSort, Order)
'Order: 1= Ascending, 2= Descending
Dim strTemp As String
Dim i As Long
Dim j As Long
Dim lngMin As Long
Dim lngMax As Long
lngMin = LBound(ArrToSort)
lngMax = UBound(ArrToSort)
For i = lngMin To lngMax - 1
For j = i + 1 To lngMax
If Order = 1 Then
If ArrToSort(i) > ArrToSort(j) Then
strTemp = ArrToSort(i)
ArrToSort(i) = ArrToSort(j)
ArrToSort(j) = strTemp
End If
ElseIf Order = 2 Then
If ArrToSort(i) < ArrToSort(j) Then
strTemp = ArrToSort(i)
ArrToSort(i) = ArrToSort(j)
ArrToSort(j) = strTemp
End If
Else
MsgBox "Input 1 for Ascending, 2 for Descending sort!" & vbLf & "Array not sorted!", vbOKOnly, "Error"
Exit Function
End If
Next j
Next i
End Function