Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code : Function SortArray(MyArray As Variant, intS

    Two years ago I had problems getting the WordBasic sort to function correctly in VBA. I'm still not sure that I've got it right, but this example seems to work.

    I remember a debate surrounding WordBasic.SortArray. I'd not made much use of it in WordBasic, but the ILINK BBS conference I was in bemoaned the fact that it wasn't present in Word97/VBA.

    I need to sort arrays prior to running utility functions, such as the ToDo function for the Bookmarks suite.


    <pre>Public Function SortArray(MyArray As Variant, intSeq As Integer,_
    intType As Integer, intKey As Integer)
    Dim lngLim As Long
    lngLim = UBound(MyArray, 1) - 1
    ' array, order 1= descend, from, to, type 0 = rows, key 1=2nd col
    WordBasic.SortArray MyArray, intSeq, 0, lngLim, intType, intKey
    'Sub TESTSortArray()
    'Dim ar(3, 3) As String
    'ar(0, 0) = "a" ' Initially in sequence
    'ar(0, 1) = "b"
    'ar(0, 2) = "c"
    'ar(1, 0) = "d"
    'ar(1, 1) = "e"
    'ar(1, 2) = "f"
    'ar(2, 0) = "g"
    'ar(2, 1) = "h"
    'ar(2, 2) = "i"
    'Call SortArray(ar, 1, 0, 0) ' descending column 1
    'Call SortArray(ar, 0, 0, 0) ' ascending column 1
    'Call SortArray(ar, 1, 0, 2) ' descending column 2
    'Call SortArray(ar, 0, 0, 2) ' ascending column 2
    'End Sub
    End Function
    </pre>


  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code : Function SortArray(MyArray As Variant, intS

    Chris,

    I was going to comment on not using named parameters, but I note (in <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/SortArray.htm>this article</A> that the Wordbasic.SortArray (unlike VBA commands) does not support named arguments.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Code : Function SortArray(MyArray As Variant, intS

    If you'd like to come into the current world, here's a routine from the Office 2000 code librarian. Be aware, however, that it uses ...RECURSION!

    <pre>Function QuickSortArray(avarArrFiles As Variant, _
    Optional intFirst As Integer = -1, _
    Optional intLast As Integer = -1) As Variant
    ' QuickSort algorithm used to sort the files
    ' in the avarArrFiles array.
    ' From CodeLibrarian
    Dim intLow As Integer
    Dim intHigh As Integer
    Dim intMiddle As Integer
    Dim varTempVal As Variant
    Dim varTestVal As Variant

    If intFirst = -1 Then intFirst = LBound(avarArrFiles)
    If intLast = -1 Then intLast = UBound(avarArrFiles)

    If intFirst < intLast Then
    intMiddle = (intFirst + intLast) / 2
    varTestVal = avarArrFiles(intMiddle)
    intLow = intFirst
    intHigh = intLast
    Do
    Do While avarArrFiles(intLow) < varTestVal
    intLow = intLow + 1
    Loop
    Do While avarArrFiles(intHigh) > varTestVal
    intHigh = intHigh - 1
    Loop
    If (intLow <= intHigh) Then
    varTempVal = avarArrFiles(intLow)
    avarArrFiles(intLow) = avarArrFiles(intHigh)
    avarArrFiles(intHigh) = varTempVal
    intLow = intLow + 1
    intHigh = intHigh - 1
    End If
    Loop While (intLow <= intHigh)
    If intFirst < intHigh Then QuickSortArray _
    avarArrFiles, intFirst, intHigh
    If intLow < intLast Then QuickSortArray _
    avarArrFiles, intLow, intLast
    End If
    End Function</pre>

    Charlotte

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code : Function SortArray(MyArray As Variant, intS

    Yup. Thanks. I have a few hacked versions of this which I first saw posted by Mike Shaffer on PlanetSource.

    My hacked versions support mutiple columns (sort by any column) and similar niceties. I think the bulk of my mods were initaited by my desire to sort ListBoxes.


    >If you'd like to come into the current world

    That's why it's taking me so long to post my Bookmarks and Tables suites. I'm converting all the WordBasic calls to VBA.

    I seem to rcall a conversation in another world (ILINK) to the effect that the WordBasic sort was still useful, perhaps because it included keys and sequences.

Posting Permissions

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