Variable Problem.
On Sun, 22 Jan 2006 08:52:02 -0800, Tim wrote:
Hi folks,
I need a help for my top problem. I spend a lot of time but I can't find a
solution for it. The following is my sample of my code. I want to sort the
value of str in ascending and the max value of str. Any help will be
appreciated.
Thanks in advance.
Tim.
Example:
Dim str As String
str = ("9, 5, 8, 3, 1, 6")
MsgBox str
Here's one way, if you have VBA6 or later:
=================================
Option Explicit
Sub foo()
Dim str As String
Dim TempArray As Variant, Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer
str = ("9, 5, 8, 3, 1, 6")
TempArray = Split(str, ", ")
' Loop until no more "exchanges" are made.
Do
NoExchanges = True
' Loop through each element in the array.
For i = 0 To UBound(TempArray) - 1
' If the element is greater than the element
' following it, exchange the two elements.
If TempArray(i) TempArray(i + 1) Then
NoExchanges = False
Temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = Temp
End If
Next i
Loop While Not (NoExchanges)
str = Join(TempArray, ", ")
MsgBox (str & vbLf & "Max Value: " & TempArray(UBound(TempArray)))
End Sub
===============================
--ron
|