Consulting

Results 1 to 10 of 10

Thread: Solved: Why is the sorted array Not being returned?

  1. #1
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location

    Solved: Why is the sorted array Not being returned?

    I want to sort the contents of individual cells: each cell contains between 0 and 16 codes in a comma-delimited string. The accompanying workbook contains my code for doing this. The only problem is that Sub BubbleSort(myArray as Variant) fails to return (or my Sub SortStdsString2() fails to receive) the sorted array.

    On Sheet1, the source string in A1; the program copies the string to C1 before it begins slicing and dicing. Beneath that is my debug listing (captured in View Immediate window. There appears to be no problems loading the initial array (called myArry) and passing it into BubbleSort, where the array name is myArray. And, BubbleSort sorts the array as expected, ordering from lowest to highest.

    The problem appears when I list the members of myArry immediately after returning to SortStdsString2 from BubbleSort. The array as displayed is no different from the array originally passed in to BubbleSort.

    I'm puzzled. Am I doing something wrong or forgetting to do something?

    Thanks in advance!
    Ron
    Windermere, FL

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I get a compile error on this line

    StdsStr = myArry

    Type Mismatch.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hello,

    I believe the short answer is that you don't return 'MyArray' back from BubbleSort.

    [VBA]BubbleSort (myArry)
    Debug.Print "bubble sort done"[/VBA]

    I couldn't see the need for the branching (GoTo), so I changed it up a bit (see 'basModuleThree'). I got a bit sloppy, but hopefully this helps :-)

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Try this if you are on a Windows Machine, you might want to look at Split and Join.

  5. #5
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    GTO, thanks for pointing out that I needed to convert Sub BubbleSort into Public Function BubbleSort and assign myArray() to BubbleSort() before exiting back to Sort_Stds.

    I cleaned up and simplified my original code for splitting the source string, I converted it to a Do While loop with a Case Select; those dreaded and feared GOTOs are gone.

    Mikerickson, good suggestions but this code has to run on my Mac Desktop at work so I've not incorporated split/join.

    Here is my code in its latest incarnation:[vba]Option Explicit

    Sub Sort_String() ' (ByVal Target As Excel.Range)

    Dim StdsStrIn As String
    Dim StdsStrOut As String
    Application.EnableEvents = False

    StdsStrIn = Cells(1, 1)
    Cells(1, 3) = StdsStrIn

    ReDim myArry(20)
    Dim my_i As Long
    Dim my_j As Long
    If Len(StdsStrIn) = 0 Then Exit Sub
    my_i = -1
    Do While Len(StdsStrIn) > 0
    my_i = my_i + 1
    my_j = InStr(1, StdsStrIn, ",")
    Select Case my_j
    Case 0
    myArry(my_i) = StdsStrIn
    StdsStrIn = ""
    Case Else
    myArry(my_i) = Trim(Left(StdsStrIn, my_j - 1))
    StdsStrIn = Trim(Mid(StdsStrIn, my_j + 2, Len(StdsStrIn) - (my_j + 1)))
    End Select
    Loop
    ReDim Preserve myArry(my_i)

    myArry = BubbleSort(myArry)

    For my_i = 0 To UBound(myArry)
    Select Case my_i
    Case 0
    StdsStrOut = myArry(my_i)
    Case Else
    StdsStrOut = StdsStrOut & ", " & myArry(my_i)
    End Select
    Next my_i

    Cells(1, 1) = StdsStrOut
    Application.EnableEvents = True

    End Sub

    Public Function BubbleSort(MyArray As Variant)
    '// courtesy of DRJ via MDMcKillop
    Dim First As Integer
    Dim Last As Integer
    Dim i As Integer
    Dim j As Integer
    Dim Temp As String
    Dim List As String

    First = LBound(MyArray)
    Last = UBound(MyArray)
    For i = First To Last - 1
    For j = i + 1 To Last
    If MyArray(i) > MyArray(j) Then
    Temp = MyArray(j)
    MyArray(j) = MyArray(i)
    MyArray(i) = Temp
    End If
    Next j
    Next i
    BubbleSort = MyArray
    End Function

    [/vba]
    Last edited by RonMcK; 09-27-2008 at 11:46 AM.
    Ron
    Windermere, FL

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Glad to be of help Ron. Thanks for posting back too :-) I just couldn't get my head into the do loop this morning, as well as knew there had to be a way of not having to create/clean-up the leading/trailing slop in the return string that I had. Very nice/crisp! Mark

  7. #7
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location

    Another way to parse the string of codes

    The following code can be substituted for the purple block of code in my program, above.
    [vba] Dim LenStd As Long
    Dim NumStds As Long

    LenStd = InStr(1, StdsStrIn, ",") - 1
    If LenStd = -1 Then LenStd = Len(StdsStrIn)
    NumStds = Int(Len(StdsStrIn) / (LenStd + 2) + 0.5)

    For my_i = 0 To NumStds - 1
    myArry(my_i) = Mid(StdsStrIn, 1 + (LenStd + 2) * my_i, LenStd)
    Next my_i

    my_i = 0
    ReDim Preserve myArry(NumStds - 1)
    [/vba] Cheers,
    Ron
    Windermere, FL

  8. #8
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Hi All,

    Well, this solution didn't work on my Mac here at work. I apparently was blinded by the parentheses; this led us all of into forcing BubbleSort() to be a Function. The following change in Sort_String() reverts us to 'calling' BubbleSort() as a Sub. The Mac has no problem with this.[vba] ' myArry = BubbleSort(myArry) '// 'Can't Assign to Array' according to XL 2004 on Mac (works on PC w/ XL2002)
    BubbleSort myArry '// Look! not parentheses around array being passed.[/vba][vba]Sub BubbleSort(myArry As Variant)
    '// courtesy of DRJ via MDMcKillop
    Dim First As Integer
    Dim Last As Integer
    Dim i As Integer
    Dim j As Integer
    Dim Temp As String
    Dim List As String

    First = LBound(myArry)
    Last = UBound(myArry)
    For i = First To Last - 1
    For j = i + 1 To Last
    If myArry(i) > myArry(j) Then
    Temp = myArry(j)
    myArry(j) = myArry(i)
    myArry(i) = Temp
    End If
    Next j
    Next i
    End Sub[/vba]
    Ron
    Windermere, FL

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Mac uses VB v.5 and Windows uses v.6, the syntax for a function returning an array is one of the differences between them.

    Don't let your boss "upgrade" to Excel 2008, it doesn't support VB. If they want to upgrade Office, they should go the BootCamp/Windows/Office 2007 route.

  10. #10
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Mikerickson,

    Thanks for the explanation, that helps my tired brain a whole lot. The non-linearity was bugging me.

    The company has about 500+ Macs in this building; I rather doubt we'll upgrade to 2008, more likely wait for the next version which rumor has it will have VBA restored as a feature. Most of our Macs are not wintels, so, bootcamp, etc is not an option.

    Cheers,

    Ron
    Ron
    Windermere, FL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •