Consulting

Results 1 to 6 of 6

Thread: Sorting inside single cell

  1. #1
    VBAX Newbie
    Joined
    Mar 2007
    Posts
    5
    Location

    Sorting inside single cell

    I have up to 40(numbers) values in 1 cell separated by space ( exemple 1243 724 13 6405 523 and so on) and need to sort them in ascending order ( 13 523 724 1243 ) inside that cell. Is it possible? I have Exel 2002

  2. #2
    VBAX Newbie
    Joined
    Mar 2007
    Posts
    5
    Location
    Quote Originally Posted by Eugene
    I have up to 40(numbers) values in 1 cell separated by space ( exemple 1243 724 13 6405 523 and so on) and need to sort them in ascending order ( 13 523 724 1243 ) inside that cell. Is it possible? I have Exel 2002
    Or, if I sort the numbers in separate cells in ascending order, how can I put them in 1 cell separated by space?

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by Eugene
    I have up to 40(numbers) values in 1 cell separated by space ( example 1243 724 13 6405 523 and so on) and need to sort them in ascending order ( 13 523 724 1243 ) inside that cell. Is it possible? I have Excel 2002
    If you download and install the free Morefunc.xll add-in then you can use this formula below. Input formula in cell B1 and copied down. The formula is an-array need to hold down:

    Ctrl,Shift,Enter

    =TRIM(MCONCAT(VSORT(EVAL("{"&SUBSTITUTE(A1," ",";")&"}"),,1)
    &" "))

    Hope it helps!
    Last edited by Aussiebear; 11-26-2024 at 03:31 AM.
    SHAZAM!

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Eugene are you looking for a VBA solution or worksheet solution? As for the worksheet solution if you have the values sorted in column A, put in the cell you want
    =A1 & " " & A2 & " " & A3 & " " & A4
    and so on.

    I personally don't know how to sort within a cell. Sorry.

    Good Luck
    Gary
    Last edited by Aussiebear; 11-26-2024 at 03:32 AM.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Sub SortCell()
        Dim MyList, m, str As String
        MyList = Split(Trim(Range("A1")), " ")
        'Sort the array
        Quick_Sort MyList, 0, UBound(MyList)
        For Each m In MyList
            str = str & m & " "
        Next
        Range("A2") = str
    End Sub
      
    Private Sub Quick_Sort(ByRef SortArray As Variant, ByVal First As Long, ByVal Last As Long)
        Dim Low As Long, High As Long
        Dim Temp As Variant, List_Separator As Long
        Low = CLng(First)
        High = CLng(Last)
        List_Separator = SortArray((First + Last) / 2)
        Do
            Do While (CLng(SortArray(Low)) < List_Separator)
                Low = Low + 1
            Loop
            Do While (CLng(SortArray(High)) > List_Separator)
                 High = High - 1
            Loop
            If (Low <= High) Then
                Temp = SortArray(Low)
                SortArray(Low) = SortArray(High)
                SortArray(High) = Temp
                Low = Low + 1
                High = High - 1
            End If
        Loop While (Low <= High)
        If (First < High) Then Quick_Sort SortArray, First, High
        If (Low < Last) Then Quick_Sort SortArray, Low, Last
    End Sub
    Last edited by Aussiebear; 11-26-2024 at 03:35 AM.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Newbie
    Joined
    Mar 2007
    Posts
    5
    Location
    Thanks everyone for help. I'll try

Posting Permissions

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