13,145,568 members (45,358 online)
Rate this:
See more:
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```
Posted 22-Nov-12 21:07pm
deus_ex198
Updated 23-Nov-12 7:49am
v12
Richard MacCutchan 23-Nov-12 6:30am

Can you post the "freak out" video on YouTube, it's bound to be funnier than what we read in the SoapBox.
Richard MacCutchan 23-Nov-12 7:55am

OK, so you have edited your question, but you still have not explained what is wrong. What happens when you click either of the buttons, what results do you see, what results do you want to see? If you expect people to help you then you need to provide as much information as possible.
deus_ex 23-Nov-12 8:07am

I'm expecting them to be sorted nicely. The numbers are written randomly. They must be sorted in columns in ascending order in the first matrix and in the second matrix in descending order.
Here is the visual example - http://i49.tinypic.com/i4plkn.jpg
Joan Murt 23-Nov-12 11:12am

Are you serious when you say that you can't remember the original task? :O
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...
Richard MacCutchan 23-Nov-12 11:41am

Hint: look up the `Sort` command in Excel.
deus_ex 23-Nov-12 13:42pm

No, I need the script written in Visual Basic. This is not about Excel, this is about VB.
Richard MacCutchan 23-Nov-12 14:45pm

No, it's about Excel, the fact that you wish to use VBA is incidental. And studying the VBA Sort method may actually help you to solve your problem. But hey, what would I know?
Abhishek Pant 23-Nov-12 14:17pm

did you posted your final and correct question now? as your question is changing time by time..
deus_ex 23-Nov-12 23:42pm

Yes

Rate this:

## Solution 1

We can't.

How do you expect us to "write the corrected code in 12 hours" when your reported problem is "it's not working properly"?

We don't don't even know what it is supposed to do, much less what it is doing that it shouldn't, or not doing that it should. It's uncommented, lazy names, and porrly indented.

So you will have to freak out.
Rate this:

## Solution 2

Okay the solution is no longer needed

Top Experts
Last 24hrsThis month
 OriginalGriff 360 CPallini 195 ppolymorphe 150 Richard Deeming 120 Pete O'Hanlon 70
 OriginalGriff 5,810 Graeme_Grant 5,051 ppolymorphe 1,984 Jochen Arndt 1,874 CPallini 1,755