PDA

View Full Version : Sum two numbers from bottom to top in a random list



N3K
07-23-2006, 04:25 AM
http://i11.photobucket.com/albums/a187/NicholasFoo/Huffman.jpg

May I know how to sum two numbers from the bottom to top in a random list and forward the extra one at the top, please? Then, compares those numbers to get the biggest one and place it on the top or in descending. I need this solution urgently :( . Hope you guy can help me : pray2: . Thanks.

OBP
07-23-2006, 06:14 AM
Try searching the forum for "Sort" or "Bubble Sort"

N3K
07-23-2006, 07:28 PM
I m very new to VBA because i just start to use it for a couple of weeks. I try to find in sort but i didn't get what i want. Can someone provide some more useful information or the code, please?

jindon
07-24-2006, 09:17 PM
try (not tested)

Sub test()
Dim a, i As Long, n As Long
a = Application.InputBox("Select range",type:=8)
Redim Preserve a(1 to UBound(a,1), 1 To 2)
i = UBound(a,1) : n = 1
Do While i >= 1
If n Mod 2 = 0 Then
a(i,2) = a(i,1) + a(i-1,1)
Else
a(i,2) = "n/a"
End If
i = i - 1 : n = n + 1
Loop
VSrotMA a, 1, UBound(a,1), 2
With Range("h2")
For i = 1 To UBound(a,1)
If a(i,2) = "n/a" Then Exit For
.Offset(n).Value = a(i,2)
n = n + 1
If n > 1 Then n = n + 1
Next
End With
End Sub

Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, temp
Dim i As Long, ii As Long, iii As Long
i = UB : ii = LB
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
Do While ary(ii, ref) < M
ii = ii + 1
Loop
Do While ary(i, ref) > M
i = i - 1
Loop
If ii <= i Then
For iii = LBound(ary, 2) To UBound(ary, 2)
temp = ary(ii, iii) : ary(ii, iii) = ary(i, iii) : ary(i,iii) = temp
Next
ii = ii + 1 : i = i - 1
End If
Loop
If LB < i Then VSortMA ary, LB, i9, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub

N3K
07-31-2006, 08:46 PM
i copy and paste the code to Sheet 1 code but encountered a compile error "sub or function not defined". can anyone give an example in .xls, please? thanks

jindon
07-31-2006, 09:03 PM
Post your entire code in Sheet1 module