+ Reply to Thread
Results 1 to 12 of 12

Sort rows by numbers that are preceded by a letter

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2015
    Location
    Connecticut
    MS-Off Ver
    2010
    Posts
    1

    Sort rows by numbers that are preceded by a letter

    How do I sort rows according to a logical numerical sequence with a letter preceding the number?
    I want to achieve this sequence of rows:
    P1
    P2
    P3
    P4
    P5
    P6
    P7
    P8
    P9
    P10
    P11
    P12

    Instead Excel sorts like this:
    P1
    P10
    P11
    P12
    P2
    P3
    P4
    P5
    P6
    P7
    P8
    P9

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,051

    Re: Sort rows by numbers that are preceded by a letter

    Can you make the entires P01, P02 P11 etc?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Sort rows by numbers that are preceded by a letter

    Hi Heyoka

    Welcome to the Forum!!

    You could create a Helper Column that strips the "P" from the Cell Values and Sort on the Helper Column or, alternately (depending on Volume), create a Custom Sort.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Sort rows by numbers that are preceded by a letter

    ="P"&TEXT(RIGHT(F8,LEN(F8)-1),"00") and drag down.

    After that you can sort on that column.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  5. #5
    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: Sort rows by numbers that are preceded by a letter

    Welcome to the board.

    You could use a UDF to pad the numbers to equal length, then sort on that column:

    Row\Col
    A
    B
    C
    1
    P1 P01 B1: =PadNum(A1, 2)
    2
    P10 P10
    3
    P11 P11
    4
    P12 P12
    5
    P2 P02
    6
    P3 P03
    7
    P4 P04
    8
    P5 P05
    9
    P6 P06
    10
    P7 P07
    11
    P8 P08
    12
    P9 P09


    Function PadNum(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.,
      '   PadNum("13A1U3", 2)    = "13A01U03"
      '   PadNum("1.2.3.15", 3)  = "001.002.003.015"
    
      ' Numbers are not shortened below their minimal representation:
      '   PadNum("1.123.2.3", 2) = "01.123.02.03"
    
      ' Returns unpadded values if iLen = 1 or omitted
      '   PadNum("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 sNum          As String
      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   ' the +1 flushes a trailing number
        sChr = Mid(sInp, iChr, 1)
        If sChr Like "#" Then
          bNum = True
          sNum = sNum & sChr
        Else
          If bNum Then
            bNum = False
            PadNum = PadNum & Format(CDbl(sNum), sFmt)
            sNum = vbNullString
          End If
          PadNum = PadNum & sChr
        End If
      Next iChr
    End Function
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Sort rows by numbers that are preceded by a letter

    @shg

    I like it...it's in my Tool Box...with proper attribution...

  7. #7
    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: Sort rows by numbers that are preceded by a letter

    Quote Originally Posted by jaslake View Post
    @shg

    I like it...it's in my Tool Box...with proper attribution...
    Glad you like it, John. I use it frequently.

  8. #8
    Registered User
    Join Date
    06-13-2013
    Location
    Charleston, SC
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: Sort rows by numbers that are preceded by a letter

    I am really liking this function also but have a followup question. It seems that when you have the same letters preceeding the number (but a different number scheme), it is not sorting like you would expect. BSD48xxx and BSD62XXX should come before BSD175XX:

    BSD13-8.1
    BSD13-13
    BSD13-14
    BSD175-1
    BSD175-4
    BSD48-1
    BSD48-2
    BSD48-8
    BSD62-1
    BSD62-1.1

    Hank

  9. #9
    Registered User
    Join Date
    01-30-2013
    Location
    Hobart, TAS
    MS-Off Ver
    Office 2003, XL2007
    Posts
    46

    Re: Sort rows by numbers that are preceded by a letter

    Col A is list
    In column B cell 1 enter =Value(Substitute(A1,"P","")) copy down to match list.
    Sort both columns on col B Ascending.
    Delete / clear col B if of no further use.

  10. #10
    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: Sort rows by numbers that are preceded by a letter

    You need to pad for the largest number:

    Row\Col
    A
    B
    C
    1
    Input
    Sort by
    2
    BSD13-8.1 BSD013-008.001 B2: =PadNum(A2, 3)
    3
    BSD13-13 BSD013-013
    4
    BSD13-14 BSD013-014
    5
    BSD48-1 BSD048-001
    6
    BSD48-2 BSD048-002
    7
    BSD48-8 BSD048-008
    8
    BSD62-1 BSD062-001
    9
    BSD62-1.1 BSD062-001.001
    10
    BSD175-1 BSD175-001
    11
    BSD175-4 BSD175-004

  11. #11
    Registered User
    Join Date
    06-13-2013
    Location
    Charleston, SC
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: Sort rows by numbers that are preceded by a letter

    ah ha, I knew you would know the answer!! Thx.

  12. #12
    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: Sort rows by numbers that are preceded by a letter

    You're welcome.

+ 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. [SOLVED] Remove numbers from a string that are preceded by a space
    By joevan1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-19-2015, 03:14 AM
  2. Replies: 8
    Last Post: 06-03-2013, 05:34 PM
  3. sort numbers in numeric form in a cell and down the rows
    By system in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-09-2012, 12:15 AM
  4. Replies: 4
    Last Post: 06-05-2006, 01:10 PM
  5. How do I sort numbers in a table (ie. rows & columns)?
    By Twinkle17 in forum Excel General
    Replies: 2
    Last Post: 12-02-2005, 01:00 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