Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    2,328
    Thanks
    0
    Thanked 1 Time in 1 Post

    Sorting strings in reverse order (Any)

    I have a column of string values (letters and digits), which I need to sort in reverse order:

    aaa
    baa
    ...
    aba
    bba
    ...
    aab
    bab
    ...
    abb
    bbb
    ...

    I cannot find a VBA script (macro) to do so. Please help.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Sorting strings in reverse order (Any)

    Try this. You should select the cells you want to sort before running the macro.
    <code>
    Sub SortReverse()
    Dim rng As Range
    Dim cel As Range
    Set rng = Selection
    rng.Offset(0, 1).EntireColumn.Insert
    For Each cel In rng
    cel.Offset(0, 1) = StrReverse(cel)
    Next cel
    rng.Resize(ColumnSize:=2).Sort Key1:=rng.Cells(1, 2)
    rng.Offset(0, 1).EntireColumn.Delete
    End Sub</code>

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Sorting strings in reverse order (Any)

    Or a slightly different version
    <code>
    Sub SortReverse()
    Dim rng As Range
    Dim cel As Range
    Set rng = Selection
    For Each cel In rng
    cel = StrReverse(cel)
    Next cel
    rng.Sort Key1:=rng.Cells(1, 1)
    For Each cel In rng
    cel = StrReverse(cel)
    Next cel
    End Sub</code>

  4. #4
    Silver Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    2,328
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Sorting strings in reverse order (Any)

    Thank you very much, indeed.

Posting Permissions

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