Consulting

Results 1 to 8 of 8

Thread: Solved: Alphanumeric Sort

  1. #1
    VBAX Contributor
    Joined
    Feb 2009
    Posts
    103
    Location

    Question Solved: Alphanumeric Sort

    hi, would like to sort an alphanumeric data, can anyone help on how to achieve this..basically, i have a raw data like C1, C10, C100, C11, C12, etc..so when sorted it's output should be in ascending order .. like C1, C10, C11, C12, C100..Etc.. thanks in advance... my data is like this:


    RAW DATA
    C1
    C10
    C102
    C103
    C104
    C105
    C106
    C107
    C108
    C109
    C11
    C110
    C100
    C111
    C112
    C113
    C114
    C115
    C116
    C117
    C118
    C119
    C12
    C101
    C120
    C122
    C1200
    C121

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I would convert your raw data to a sortable format eg C0001
    This should help accomplish that task, (assuming your data is representative)
    =LEFT(A1,1) & TEXT(RIGHT(A1,LEN(A1)-1),"0000")
    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'

  3. #3
    VBAX Contributor
    Joined
    Feb 2009
    Posts
    103
    Location

    Smile

    Hi, mdmackillop..thanks for the reply..i tried to modified the formula that you suggested as: =(RIGHT(A2,LEN(A2)-1)) and =LEFT(A1,1), so i can separate the letter and the number..but when i tried to sort the number it shows this error..and i have tried formatting the cell to a number but it doesn't help... any ideas? thanks..
    here's the print screen of the error..

  4. #4
    VBAX Contributor
    Joined
    Feb 2009
    Posts
    103
    Location
    Hi, mdmackillop, thanks i find a work around on this.. the formula you suggested helps a lot...what i did was like this:
    =RIGHT(A1,(LEN(A1)-1)) 'to get the numbers
    'then multiply the data to 1 to convert it to numbers then sort using excel
    =LEFT(A1,1) & TEXT(RIGHT(B1,LEN(B1)),"0") 'to combined the letters again
    it sorts perfectly..

    but is there a simpler way to do this? thank you..

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Hi joms, The thing to remember here is that your initial data was stored as Text, and that it will remain as Text even after removing the alphabetical 1st character.

    Using the data in your graphic as an example:
    =Left(A1,1) on C101 will return C

    whereas using
    =Right(A1,Len(A1)-1 on C101 will return 101 ( the 101 value is still regarded as text. See in your example how the string is stored to the left of the cell?

    However using
    =Right(A1,Len(A1)-1)*1 on C101 will return 101 as a number, which will be stored to the right side of the cell.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Joms,

    Not sure if this is of any help, but if you cannot convert the data, maybe a short macro? This would still require the helper column, so Col B would be used (or temp inserted/deleted)

    [vba]
    Option Explicit
    Sub EX()
    Dim lLRow As Long
    Dim rng As Range
    Dim ary
    Dim i As Long

    '// Starts in row 2, change to suit.//
    Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)

    ary = Application.Transpose(rng.Value)

    For i = LBound(ary) To UBound(ary)
    ary(i) = Right(ary(i), Len(ary(i)) - 1)
    Next

    rng.Offset(, 1).Value = Application.Transpose(ary)

    Set rng = rng.Resize(, 2)
    rng.Sort Key1:=rng(1, 2), Order1:=xlAscending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    Set rng = rng.Resize(, 1).Offset(, 1)
    rng.ClearContents
    End Sub
    [/vba]

    Hope this helps,

    Mark

  7. #7
    VBAX Contributor
    Joined
    Feb 2009
    Posts
    103
    Location
    hi, mdmackillop, Aussiebar and GTO...thanks for the help.. Cheers

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by joms
    Hi, mdmackillop, thanks i find a work around on this.. the formula you suggested helps a lot...what i did was like this:
    =RIGHT(A1,(LEN(A1)-1)) 'to get the numbers
    'then multiply the data to 1 to convert it to numbers then sort using excel
    =LEFT(A1,1) & TEXT(RIGHT(B1,LEN(B1)),"0") 'to combined the letters again
    it sorts perfectly..

    but is there a simpler way to do this? thank you..
    Yes.
    Just use the formula I gave you to put the data in one column and sort on that column, unless of course your data is not really as presented.
    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'

Posting Permissions

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