Consulting

Results 1 to 3 of 3

Thread: Solved: Alphabetically reverse text in cell

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Posts
    32
    Location

    Solved: Alphabetically reverse text in cell

    Hello,

    Could I bother someone to show me a way to search up a column and reorder text in each cell alphabetically?

    Example: Data is in column E

    Text in each cell is separated by a space:

    ABTRSW AARTIS

    The cell example above should be rearranged to:

    AARTIS ABTRSW

    Thanks,

    lhardee

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Dim i As Long
    Dim LastRow As Long
    Dim aryNames As Variant

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = 1 To LastRow

    aryNames = Split(.Cells(i, "A"), " ")
    .Cells(i, "A").Value = Join(BSort(aryNames), " ")
    Next i
    End With

    End Sub

    Public Function BSort(InVal As Variant, Optional Order As String = "Asc")
    Dim fChanges As Boolean
    Dim iElement As Long
    Dim iElement2 As Long
    Dim temp As Variant
    Dim ToSort

    ToSort = InVal
    Do
    fChanges = False
    For iElement = LBound(ToSort) To UBound(ToSort) - 1
    If ((Order = "Asc" And ToSort(iElement) > ToSort(iElement + 1)) Or _
    (Order <> "Asc" And ToSort(iElement) < ToSort(iElement + 1))) Then
    'Swap elements
    temp = ToSort(iElement)
    ToSort(iElement) = ToSort(iElement + 1)
    ToSort(iElement + 1) = temp
    fChanges = True
    End If
    Next iElement
    Loop Until Not fChanges
    BSort = ToSort
    End Function
    [/vba]
    ____________________________________________
    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
    Feb 2008
    Posts
    32
    Location
    WOW xld,

    That was a fast response !!! Thank you very much.

    Perfect, by the way.



    lhardee

Posting Permissions

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