+ Reply to Thread
Results 1 to 8 of 8

Sort Text ascending / descending from a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2017
    Location
    London
    MS-Off Ver
    2007
    Posts
    4

    Sort Text ascending / descending from a cell

    Hi There,

    Need help in finding a formula to arrange text ascending and descending from each cell.

    Eg: Cell A1 has "EDDIE" , results in cell B1 (ascending) should be"DDEEI" and results in cell C1 (descending ) should be "IEEDD"

    Thanks

  2. #2
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,407

    Re: Sort Text ascending / descending from a cell

    Hi eddiedhs. Welcome to the forum.

    I would be surprised if this can be done without VBA.

    Try this user defined function by tigeravatar. If you are not familiar with how to install VBA code see the instructions following this code.

    'tigeravatar ExcelForum
    Public Function ConcatAll(ByVal varData As Variant, Optional ByVal sDelimiter As String = vbNullString, Optional ByVal bUnique As Boolean = False) As String
    'Created by TigerAvatar at www.excelforum.com, September 2012
    'Purpose is to concatenate many strings into a single string
    'Can be used with arrays, range objects, and collections
        
        Dim DataIndex As Variant    'Used to loop through arrays, range objects, and collections
        Dim strResult As String     'Used to build the result string
        
        'Test if varData is an Array, Range, or Collection
        If IsArray(varData) _
        Or TypeOf varData Is Range _
        Or TypeOf varData Is Collection Then
            
            'Found to be an, array, range object, or collection
            'Loop through each item in varData
            For Each DataIndex In varData
                'Check if the item isn't empty
                If Len(DataIndex) > 0 Then
                    'Found the item isn't empty, check if user specified bUnique as True
                    If bUnique = True Then
                        'bUnique is true, check if the item has been included in the result yet
                        If InStr(1, "||" & strResult & "||", "||" & DataIndex & "||", vbTextCompare) = 0 Then
                            'Item has not been included in the result, add item to the result
                            strResult = strResult & "||" & DataIndex
                        End If
                    Else
                        'bUnique is not true, add item to the result
                        strResult = strResult & "||" & DataIndex
                    End If
                End If
            Next DataIndex
            
            'Correct strResult to remove beginning delimiter and convert "||" to the specified sDelimiter
            strResult = Replace(Mid(strResult, 3), "||", sDelimiter)
            
        Else
            'Found not to be an array, range object, or collection
            'Simply set the result = varData
            strResult = varData
        End If
        
        'Output result
        ConcatAll = strResult
        
    End Function
    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    Then try array entering this formula in B1. If you aren’t familiar with array-entered formulas array enter means the formula must be committed from edit mode by simultaneously pressing and holding down Ctrl and Shift while hitting Enter.

    Formula: copy to clipboard
    =concatall(CHAR(SMALL(CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)),ROW(INDIRECT("1:"&LEN(A1))))),"")
    Change SMALL to LARGE to reverse the order.
    Dave

  3. #3
    Registered User
    Join Date
    11-12-2017
    Location
    London
    MS-Off Ver
    2007
    Posts
    4

    Re: Sort Text ascending / descending from a cell

    Hi FlameRetired ,

    Thanks for this, but i cannot get it work
    Edd.xlsm

  4. #4
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,407

    Re: Sort Text ascending / descending from a cell

    Quote Originally Posted by eddiedhs View Post
    Hi FlameRetired ,

    Thanks for this, but i cannot get it work
    Attachment 547551
    Your attached has the strings in proper case. The original example was in upper case. Try array entering this.
    Formula: copy to clipboard
    =concatall(CHAR(SMALL(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1)),ROW(INDIRECT("1:"&LEN(A1))))),"")

  5. #5
    Registered User
    Join Date
    11-12-2017
    Location
    London
    MS-Off Ver
    2007
    Posts
    4

    Re: Sort Text ascending / descending from a cell

    thanks a ton, it works now

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: Sort Text ascending / descending from a cell

    .
    Pure VBA method :

    Option Explicit
    
    Public Sub SortContents()
    Dim oCell As Range
    Dim I As Integer, J As Integer, iLen As Integer
    Dim strChr() As String, strWk As String
    
        For Each oCell In Selection
        strWk = oCell.Value
        iLen = Len(strWk)
        ReDim strChr(1 To iLen)
            For I = 1 To iLen
            strChr(I) = Left(strWk, 1)
            strWk = Right(strWk, Len(strWk) - 1)
            Next I
            
            For I = 1 To iLen - 1
                For J = I + 1 To iLen
                    If strChr(J) < strChr(I) Then
                        strWk = strChr(I)
                        strChr(I) = strChr(J)
                        strChr(J) = strWk
                    End If
                Next J
            Next I
                strWk = ""
                
                For I = 1 To iLen
                    strWk = strWk & strChr(I)
                Next I
                
        oCell.Value = strWk
        Next oCell
    End Sub
    Website Resource:

    https://windowssecrets.com/forums/sh...ell-(excel-xp)

  7. #7
    Registered User
    Join Date
    11-12-2017
    Location
    London
    MS-Off Ver
    2007
    Posts
    4

    Re: Sort Text ascending / descending from a cell

    Hi Logit,

    Thanks, but i feel the string is broken??

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: Sort Text ascending / descending from a cell

    .
    No problems here. Works as it should :

    Option Explicit
    
    Public Sub SortContentsAscend()
    Dim oCell As Range
    Dim I As Integer, J As Integer, iLen As Integer
    Dim strChr() As String, strWk As String
    
        For Each oCell In Selection
        strWk = oCell.Value
        iLen = Len(strWk)
        ReDim strChr(1 To iLen)
            For I = 1 To iLen
            strChr(I) = Left(strWk, 1)
            strWk = Right(strWk, Len(strWk) - 1)
            Next I
            
            For I = 1 To iLen - 1
                For J = I + 1 To iLen
                    If strChr(J) < strChr(I) Then
                        strWk = strChr(I)
                        strChr(I) = strChr(J)
                        strChr(J) = strWk
                    End If
                Next J
            Next I
                strWk = ""
                
                For I = 1 To iLen
                    strWk = strWk & strChr(I)
                Next I
                
        oCell.Value = strWk
        Next oCell
    End Sub
    
    
    Public Sub SortContentsDescend()
    Dim oCell As Range
    Dim I As Integer, J As Integer, iLen As Integer
    Dim strChr() As String, strWk As String
    
        For Each oCell In Selection
        strWk = oCell.Value
        iLen = Len(strWk)
        ReDim strChr(1 To iLen)
            For I = 1 To iLen
            strChr(I) = Right(strWk, 1)
            strWk = Left(strWk, Len(strWk) - 1)
            Next I
            
            For I = 1 To iLen - 1
                For J = I + 1 To iLen
                    If strChr(J) > strChr(I) Then
                        strWk = strChr(I)
                        strChr(I) = strChr(J)
                        strChr(J) = strWk
                    End If
                Next J
            Next I
                strWk = ""
                
                For I = 1 To iLen
                    strWk = strWk & strChr(I)
                Next I
                
        oCell.Value = strWk
        Next oCell
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Code to sort in ascending AND descending order
    By infinitecables in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2017, 11:10 AM
  2. [SOLVED] Fix Sub row label instead of using Sort by Ascending or Descending
    By so_nice3 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 08-18-2014, 02:21 AM
  3. Can you sort a chart out in Ascending/Descending Order
    By dandavis1 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 11-26-2010, 09:52 AM
  4. one macro to sort text in ascending and descending order
    By jpruffle in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-13-2008, 10:24 PM
  5. sort function - from ascending to descending
    By jimmyp in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-14-2006, 02:15 PM
  6. [SOLVED] Help on autofilter sort ascending and descending!
    By crapit in forum Excel General
    Replies: 2
    Last Post: 12-12-2005, 10:45 AM
  7. [SOLVED] Sort other than by alphabetical ascending/ descending
    By Melissa in forum Excel General
    Replies: 6
    Last Post: 09-02-2005, 03:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1