• Sorting strings in reverse order (Any)

    Author
    Topic
    #441039

    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.

    Viewing 1 reply thread
    Author
    Replies
    • #1057930

      Try this. You should select the cells you want to sort before running the macro.

      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

    • #1057932

      Or a slightly different version

      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

    Viewing 1 reply thread
    Reply To: Sorting strings in reverse order (Any)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: