Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Office Excel Homework VBA
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 22:07pm
deus_ex198
Edited 23-Nov-12 8:49am
v12
Comments
Richard MacCutchan at 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 at 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 at 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 at 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 at 23-Nov-12 11:41am
   
Hint: look up the Sort command in Excel.
deus_ex at 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 at 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 at 23-Nov-12 14:17pm
   
did you posted your final and correct question now? as your question is changing time by time..
deus_ex at 23-Nov-12 23:42pm
   
Yes
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Okay the solution is no longer needed
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 214
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 163
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 25 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100