+ Reply to Thread
Results 1 to 16 of 16

Sorting Mixed Text and Numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Sorting Mixed Text and Numbers

    I need help with a sorting problem. A column holds numbers and numbers with suffixes. The conventional sort function pushes the text values to the bottom. I need them sorted along with the numbers. Simple? Maybe not.

    Microsoft's KB says to add a column formatted as text and then to RETYPE ALL OF THE VALUES! Splendid. But my file has hundreds of records. Re-typing all of them would be a major pain. On the other hand, if I had started typing while trying to find a way to sort the darn thing I might be done by now. ;-)

    Here's what I know so far: If we create a column, format it as text and then populate with 1, 2, 3, 4, 1a, 2a, 3a, 4a, etc. it will sort exactly correct (1, 1a, 2, 2a...) after selecting the "sort numbers and numbers stored as text separately" when the Sort Warning appears. If you try to create the sample I typed here, you need to be careful that the area is formatted as text BEFORE entering the text. You apparently cannot change the format of an existing column of data to Text or if you do it does not have the desired effect when sorting. It might be helpful to know if this can be done too. MS suggests not.

    Can anybody come up with a solution to properly sort this column of data? And why is this problem causing me a daja vu feeling?

    Thanks for your suggestions.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Sorting Mixed Text and Numbers

    do all labels contain at most a 3digit number followed by a 2 letter/number?

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting Mixed Text and Numbers

    Here's a vba solution if you like.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Re: Sorting Mixed Text and Numbers

    yes, no more than three digits.

  5. #5
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Re: Sorting Mixed Text and Numbers

    Hi jindon,

    Your VBA solution works like a charm. Thanks so very much. I am somewhere between novice and intermediate in VBA but your code just lost me completely. Would you be so kind as to tell me how it works and what it is doing? Is it custom made for my exact needs or can it be applied in general to sort problems of mixed text and numbers? (See, I can't even figure that out from looking at your code! Duh.)

    For the benefit of others, I will reproduce Jindon's code here. Again, it works like a charm; I hope others will find this helpful too.

    Sub test()
        Dim a, i As Long
        With Cells(1).CurrentRegion
            a = .Value
            ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 1)
            With CreateObject("VBScript.RegExp")
                .Pattern = "\d+(\.\d+)?"
                For i = 2 To UBound(a, 1)
                    If .test(a(i, 1)) Then a(i, UBound(a, 2)) = _
                    Format$(.Execute(a(i, 1))(0), String(20, "0")) & a(i, 1)
                Next
            End With
            VSortM a, 2, UBound(a, 1), UBound(a, 2), 1
            .Value = a
        End With
    End Sub
    
    Private Sub VSortM(ary, LB, UB, ref, Optional ord As Boolean = 1)
        Dim M As Variant, i As Long, ii As Long, iii As Long, temp
        i = UB: ii = LB
        M = ary(Int((LB + UB) / 2), ref)
        Do While ii <= i
            If ord Then
                Do While ary(ii, ref) < M: ii = ii + 1: Loop
            Else
                Do While ary(ii, ref) > M: ii = ii + 1: Loop
            End If
            If ord Then
                Do While ary(i, ref) > M: i = i - 1: Loop
            Else
                Do While ary(i, ref) < M: i = i - 1: Loop
            End If
            If ii <= i Then
                For iii = LBound(ary, 2) To UBound(ary, 2)
                    temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
                Next
                ii = ii + 1: i = i - 1
             End If
        Loop
        If LB < i Then VSortM ary, LB, i, ref, ord
        If ii < UB Then VSortM ary, ii, UB, ref, ord
    End Sub

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting Mixed Text and Numbers

    Microsoft's KB says to add a column formatted as text and then to RETYPE ALL OF THE VALUES!
    Select the column, format as Text.

    Data > Text to columns, Next, Next, tick Text, finish. Now it's all text.

    Data > Sort, tick Sort numbers and numbers stored as text separately.

    Takes 10 times longer to describe than do.

    A
    1
    ToBeSorted
    2
    1
    3
    1
    4
    10
    5
    100
    6
    100
    7
    101R1
    8
    101R1
    9
    101R1
    10
    102
    11
    102
    12
    103R1
    13
    103R1
    14
    104
    Last edited by shg; 06-18-2014 at 12:16 PM.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting Mixed Text and Numbers

    1) Store the data in array, variable a.
    2) Resize the array 1) by one column at the end to use it as a key column.
    3) Loop through the array to get the first appear number(s) if there is.
    4) If the number(s) exists, convert it as 20 digits number as a string so that it can be sorted propery.
    5) then add actual value at the end of 4) to make the sort within the same numbers,(duplicates)
    6) Use "QuickSort" algorithm in "VSortM" sub procedure to sort the array.
    If you change the last argument to 0, it will sort in descending order.

    The main concept is to convert the data like

    12xxx ----> 0000000000000000001212xxx
    xyz002yy ----> 0000000000000000002xyz002yy

    so that it will be sorted properly.

    However, if you have data like

    123xxx2 -----> 00000000000000000123123xxx2
    123xxx10 ----> 00000000000000000123123xxx10

    123xxx10 comes before 123xxx2
    You see the reason in above, so it needs to adjust it further.

  8. #8
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Re: Sorting Mixed Text and Numbers

    To shg:

    Thanks for your simple solution. It ALMOST works, but it sorted 1, 10, 100 above 2. Any thoughts on how to fix that? Or did I miss something? Either way, thanks again for your input!

    To jindon:

    Thanks for your explanation. This is most helpful. I am very (albeit easily) impressed! I really appreciate your help!

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting Mixed Text and Numbers

    Ah, misunderstood. If you want the sort order shown in col A below, you can use a UDF in col B as shown, and sort by that:

    A
    B
    C
    1
    ToBeSorted Sort
    2
    1 001 B2: =PadNums(A2, 3)
    3
    2 002
    4
    3 003
    5
    4 004
    6
    5R1 005R001
    7
    6R1 006R001
    8
    7R2 007R002
    9
    8 008
    10
    9R1 009R001
    11
    10 010
    12
    11 011
    13
    12 012
    14
    13R2 013R002
    15
    14 014
    16
    15 015
    17
    16 016
    18
    17R1 017R001
    19
    17R2 017R002


    Function PadNums(sInp As String, Optional ByVal iLen As Long = 1) As String
        ' shg 2003-1115
    
        ' Expands numbers in a string to iLen characters for sorting; e.g.,
        '   PadNums("13A1U3", 2)    = "13A01A03"
        '   PadNums("1.2.3.15", 3)  = "001.002.003.015"
        
        ' Numbers are not shortened below their minimal representation:
        '   PadNums("1.123.2.3", 2) = "01.123.02.03"
        
        ' Returns unpadded values if iLen omitted
        '   PadNums("01.123.02.03") = "1.123.2.3"
        
        ' All non-numeric characters are returned as-is
        
        Dim sFmt    As String
        Dim iChr    As Long
        Dim iNum    As Long
        Dim sChr    As String
        Dim bNum    As Boolean
        
        sFmt = String(IIf(iLen < 1, 1, IIf(iLen > 15, 15, iLen)), "0")
    
        For iChr = 1 To Len(sInp) + 1
            sChr = Mid(sInp, iChr, 1)
            If sChr Like "#" Then
                bNum = True
                iNum = iNum * 10 + CLng(sChr)
            Else
                If bNum Then
                    bNum = False
                    PadNums = PadNums & Format(iNum, sFmt)
                    iNum = 0
                End If
                PadNums = PadNums & sChr
            End If
        Next
    End Function

  10. #10
    Forum Contributor
    Join Date
    10-08-2012
    Location
    San Clemente, California
    MS-Off Ver
    Office365
    Posts
    383

    Re: Sorting Mixed Text and Numbers

    Thanks shg; I will give it a try. I really appreciate your effort.

  11. #11
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: Sorting Mixed Text and Numbers

    This isn't elegant but seems to work. I have used 2 helper columns. The first helper separates the number from the text if there is a max of 3 digits before the text. The second helper separates the text from the original number. The sort is then done by selecting all the data then sort on Helper 2, and then Helper 1.

    Helper 1:

    Formula: copy to clipboard
    =IF(ISNUMBER(A2),A2,IF(ISNUMBER(--LEFT(A2,3)),--LEFT(A2,3),IF(ISNUMBER(--LEFT(A2,2)),--LEFT(A2,2),IF(ISNUMBER(--LEFT(A2,1)),--LEFT(A2,1)))))


    Helper 2:

    Formula: copy to clipboard
    =SUBSTITUTE(A2,B2,"")
    Attached Files Attached Files
    Last edited by newdoverman; 06-18-2014 at 06:58 PM.
    <---------If you like someone's answer, click the star to the left of one of their posts to give them a reputation point for that answer.
    Ron W

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting Mixed Text and Numbers

    As I explained in my previous post, mine is different from the others.

    Sort by the numbers that first appear in the cell if there is. Ignoring non numeric prefix.
    Use the following code with the sort function intact.
    Sub test()
        Dim a, i As Long, temp As String, m As Object, txt As String
        With Cells(1).CurrentRegion
            a = .Value
            ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 1)
            With CreateObject("VBScript.RegExp")
                .Pattern = "(\D*)(\d+)(.*)"
                For i = 2 To UBound(a, 1)
                    temp = a(i, 1)
                    temp = .Replace(temp, "$2$1$3")
                    Do While .test(temp)
                        txt = txt & .Replace(temp, "$1" & _
                        Format$(.Replace(temp, "$2"), String(20, "0")))
                        temp = .Replace(temp, "$3")
                    Loop
                    a(i, UBound(a, 2)) = txt & a(i, 1): txt = ""
                Next
            End With
            VSortM a, 2, UBound(a, 1), UBound(a, 2), 1
            .Value = a
        End With
    End Sub
    The above is fixed for the problem I have mentioned in my previous post.

    If you don't want to ignore non numeric prefix, just delete the line in red.
    Last edited by jindon; 06-18-2014 at 08:16 PM.

  13. #13
    Registered User
    Join Date
    08-29-2019
    Location
    Palo Alto, CA
    MS-Off Ver
    2010
    Posts
    2

    Re: Sorting Mixed Text and Numbers

    If you are still active I would love some help with this, I have the same problem and I cannot get this code to work yet.

  14. #14
    Registered User
    Join Date
    09-19-2018
    Location
    Boston, MA
    MS-Off Ver
    Office 365
    Posts
    13

    Re: Sorting Mixed Text and Numbers

    Quote Originally Posted by jindon View Post
    Here's a vba solution if you like.
    Jindon, I'm helping my Professor implement your VBA Solution, but across 16,000 columns that need to be sorted.

    See attached worksheet.

    I successfully modified Jindon's VBA Excel File which you uploaded (See the 3rd post on this thread)

    because I have like 16,000 columns to loop through.... copy & pasting into your "Test" macro.


    See "Rwat1_Update" macro.... it takes 3 seconds per Loop, but I have 16,000 columns to loop through (e.g. +26 hours or 1 days time)

    I would really appreciate your help if you can help optimize this code, and reudce it from 3 seconds down to 1 second or less? To save me lots of time...

    See attached worksheet with "Rwat1_Update" macro.
    Attached Files Attached Files
    Last edited by rwat1; 09-24-2018 at 12:18 AM.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Sorting Mixed Text and Numbers

    rwat1,

    Unfortunately your post does not comply with Rule 4 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.

  16. #16
    Registered User
    Join Date
    09-19-2018
    Location
    Boston, MA
    MS-Off Ver
    Office 365
    Posts
    13

    Re: Sorting Mixed Text and Numbers

    @jheiler1

    You can check out my thread in which Jindon responded. https://www.excelforum.com/excel-pro...and-paste.html

+ 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. text mixed in with numbers in pivot table columns.
    By td3201 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 08-24-2012, 12:54 PM
  2. Separating text and numbers from a mixed code
    By amahesh101 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-10-2010, 03:42 AM
  3. AutoFill mixed text and numbers
    By fandangle in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 12-01-2007, 09:13 AM
  4. Summation of cells with mixed numbers & text
    By HTWingNut in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-31-2007, 10:31 AM
  5. Incrementing Mixed text & numbers
    By Janet T in forum Excel General
    Replies: 8
    Last Post: 11-08-2005, 09:02 PM

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