Consulting

Results 1 to 5 of 5

Thread: Sort Alphabetical

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    7
    Location

    Smile Sort Alphabetical

    hi,


    I have an array of 5 values i need to verify the listed array is in a alphabetical order or not

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    What sort of array? A VBA array, worksheet array? And in what applicationb?
    ____________________________________________
    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
    VBAX Regular
    Joined
    Apr 2009
    Posts
    7
    Location
    Quote Originally Posted by xld
    What sort of array? A VBA array, worksheet array? And in what applicationb?
    Its an VBA Array

  4. #4
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here is an example of how to use a bubble sort on a vba array. In a standard module and run the sub "test"

    [VBA]Option Base 1
    Option Explicit
    Sub test()
    Dim arrayContacts() As Variant

    arrayContacts = Array("James", "Mary", "Tom", "Beth", "Bob", "Chris", _
    "Daniel", "Lari", "Al", "Teresa")
    Call BubbleSort(arrayContacts)

    End Sub
    Sub BubbleSort(MyArray() As Variant)
    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
    For i = 1 To UBound(MyArray)
    List = List & vbCrLf & MyArray(i)
    Next
    MsgBox List
    End Sub
    [/VBA]
    example attached.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Malcolm suggests that we show the results in the sub test rather than from the function....he has offered the following change which does just that...a good suggestion. Thanks Malcolm.
    [vba]Option Explicit
    Sub test()
    Dim arrayContacts() As Variant
    Dim i As Integer
    Dim List As String
    arrayContacts = Array("James", "Mary", "Tom", "Beth", "Bob", "Chris", _
    "Daniel", "Lari", "Al", "Teresa")

    Call BubbleSort(arrayContacts)

    For i = 1 To UBound(arrayContacts)
    List = List & vbCrLf & arrayContacts(i)
    Next
    MsgBox List

    End Sub
    Sub BubbleSort(MyArray() As Variant)
    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
    For i = 1 To UBound(MyArray)
    List = List & vbCrLf & MyArray(i)
    Next
    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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