Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

VB
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
Updated 23-Nov-12 7:49am
v12
Comments
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 M 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.

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.
 
Share this answer
 
Okay the solution is no longer needed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900