Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Variable Problem.

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Variable Problem.

Hi Ron,

Thank you very much for your code. I have another question regarding the
code. How to sort the alphabet and date? Please help.

For example:

str = ("12/1/06, 12/15/05, 1/1/05, 10/10/05")

or

str = ("Peter Smith, John betz, John bocco")

Thanks in advance.

Tim.

"Ron Rosenfeld" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Variable Problem.

On Sun, 22 Jan 2006 22:31:02 -0800, Tim wrote:

Hi Ron,

Thank you very much for your code. I have another question regarding the
code. How to sort the alphabet and date? Please help.

For example:

str = ("12/1/06, 12/15/05, 1/1/05, 10/10/05")

or

str = ("Peter Smith, John betz, John bocco")

Thanks in advance.

Tim.



What was the problem with the sort you obtained when you tried those values for
str?
--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tim Tim is offline
external usenet poster
 
Posts: 408
Default Variable Problem.

Hi Ron,

When I ran the code with the following str (date), I got the result like
this: 1/1/05, 10/10/05, 12/1/06, 12/15/05 Max Value: 12/15/05. I am
expecting 12/1/06 is the last value but it is not.

str = ("12/1/06, 12/15/05, 1/1/05, 10/10/05")

Please help.

Thanks in advance.

Tim.

"Ron Rosenfeld" wrote:

On Sun, 22 Jan 2006 22:31:02 -0800, Tim wrote:

Hi Ron,

Thank you very much for your code. I have another question regarding the
code. How to sort the alphabet and date? Please help.

For example:

str = ("12/1/06, 12/15/05, 1/1/05, 10/10/05")

or

str = ("Peter Smith, John betz, John bocco")

Thanks in advance.

Tim.



What was the problem with the sort you obtained when you tried those values for
str?
--ron

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable problem Rob Hargreaves[_2_] Excel Programming 1 September 10th 05 07:23 PM
Having a problem using a variable to add a 'name' Kobayashi[_50_] Excel Programming 1 October 29th 04 03:20 PM
Having a problem using a variable to add a 'name' Kobayashi[_49_] Excel Programming 1 October 29th 04 02:22 PM
Variable problem TC[_6_] Excel Programming 3 September 16th 04 09:39 PM
Problem trying to us a range variable as an array variable TBA[_2_] Excel Programming 4 September 27th 03 02:56 PM


All times are GMT +1. The time now is 07:43 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"