I'm trying to write code in Visual Basic for Application in MS Excel, but it's not working properly. It's for my homework.

Please write the corrected code ASAP.

I don't remember the original task, but that's what I've remembered.

There are two 4x5 matrices in Excel. The user writes some numbers randomly in the matrices and by clicking first button CommandButton1_Click()the code must sort the numbers in the first matrix ascending. By clicking the second button CommandButton2_Click() the code must sort the numbers in the second matrix descending in columns.

Here is the visual example of matrix.

http://i50.tinypic.com/2j34ho7.jpg
That's how the result should look.

http://i49.tinypic.com/i4plkn.jpg
Dim a(4, 5), b(4, 5), buff As Double
Dim i, j, k As Integer
Private Sub CommandButton1_Click()
For i = 1 To 4
For j = 1 To 5
b(i, j) = Cells(i + 9, j + 1).Value
Next j
Next i
For i = 1 To 5
For j = 1 To 4
For k = j + 1 To 4
If b(j, i) < b(k, i) Then
buff = b(k, i): b(k, i) = b(j, i): b(j, i) = buff
End If
Next k
Next j
Next i
For i = 1 To 4
For j = 1 To 5
Cells(i + 9, j + 1).Value = b(i, j)
Next j
Next i
End Sub
Private Sub CommandButton2_Click()
For i = 1 To 4
For j = 1 To 5
a(i, j) = Cells(i + 2, j + 1).Value
b(i, j) = Cells(i + 9, j + 1).Value
Next j
Next i
For k = 1 To 4
For i = 1 To 4
For j = i + 1 To 5
If a(k, i) > a(k, j) Then
buff = a(k, i): a(k, i) = a(k, j): a(k, j) = buff
End If
Next j
Next i
Next k
For i = 1 To 4
For j = 1 To 5
Cells(i + 2, j + 1).Value = a(i, j)
Next j
Next i
End Sub

Here is the visual example - http://i49.tinypic.com/i4plkn.jpg

Don't you have friends or at least people in the same class that could give you the task?

In almost 12 years here this is by far the best I've seen posted...

`Sort`

command in Excel.