+ Reply to Thread
Results 1 to 9 of 9

Sorting Alphanumeric Data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-29-2008
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    237

    Sorting Alphanumeric Data

    Ok, before any one shoots me down, i have looked around the forum and the internet for a answer, Im im half way there.

    I have a column in my worksheet which contains alphanumeric data, I also have a Custom menu option to sort the worksheet but 3 columns.

    No i know if i have a column contains the following

    1
    1
    2a
    3b
    3c
    2

    and i have set the cells to text then it would sort as follows

    1
    1
    2
    2a
    3c
    3b

    Which is cool but i have a colum looking like this

    3
    3
    3
    8
    8a
    8b
    65a
    65
    71
    71

    and when sorted looks like this

    3
    3
    3
    65
    65a
    71
    71
    8
    8a
    8b

    this is because excel is using the first digit, but i need it to sort as whole numbers so it would look like below

    3
    3
    3
    8
    8a
    8a
    65
    65a
    71
    71

    Any ideas, I need this to be included in teh VBA code i have for sorting

    Sub SortSpecial()
    Dim FirstRow As Long, EndRow As Long, LastRow As Long
    ActiveSheet.Protect Password:="gideon", UserInterfaceOnly:=True
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    FirstRow = Range("D1").End(xlDown).Row
    
    
    
        Do
            If FirstRow > LastRow Then Exit Sub
            
            EndRow = Range("D" & FirstRow).End(xlDown).Row
            Range("B" & FirstRow, "P" & EndRow).Sort Key1:=Range("D" & FirstRow), Order1:=xlAscending, _
                Key2:=Range("E" & FirstRow), Order2:=xlAscending, _
                Key3:=Range("H" & FirstRow), Order3:=xlAscending, _
                Header:=xlGuess, OrderCustom:=1, _
                MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
                DataOption2:=xlSortNormal
            FirstRow = Range("D" & EndRow).End(xlDown).Row
        Loop
        
     
        
    End Sub
    Thanks G

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Sorting Alphanumeric Data

    Simplest solution is perhaps to use an adjacent to normalise the numerics... eg if we assume your values are in A1:A10 then:

    B1: =LOOKUP(9.99E+307,--MID(A1,1,ROW(A$1:INDEX(A:A,LEN(A1)))))
    copied down

    You can then sort by B and then A.
    Last edited by DonkeyOte; 02-08-2010 at 11:01 AM. Reason: removed the TEXT - not warranted

  3. #3
    Forum Contributor
    Join Date
    01-29-2008
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    237

    Re: Sorting Alphanumeric Data

    so i would have to set up another column and then sort that column to sort the main column, does this column have to be adjacent or can it be futher down the sheet.

    Would it cope with blank cells as not all cells int he column has data ?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Sorting Alphanumeric Data

    It might be easier to post a sample with before/after sheets so people can better visualise exactly what it is you want to achieve
    (ie are blanks to be ignored or pushed to the top / bottom etc...)

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

    Arrow Re: Sorting Alphanumeric Data

    Maybe a UDF?

    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 i       As Long
        Dim iNum    As Long
        Dim sChr    As String
        Dim bNum    As Boolean
        
        If iLen < 1 Then iLen = 1
        If iLen > 15 Then iLen = 15
        sFmt = String(iLen, "0")
    
        For i = 1 To Len(sInp)
            sChr = Mid(sInp, i, 1)
            If sChr Like "#" Then
                bNum = True
                iNum = iNum * 10 + CInt(sChr)
            Else
                If bNum Then
                    PadNums = PadNums & Format(iNum, sFmt)
                    iNum = 0
                    bNum = False
                End If
                PadNums = PadNums & sChr
            End If
        Next
        If bNum Then PadNums = PadNums & Format(iNum, sFmt)
    End Function

    In B1 and copy down, =PadNums(A1, 2), then sort by column B.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    01-29-2008
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    237

    Re: Sorting Alphanumeric Data

    I have posted an example of what im trying todo.

    The example shows a before sort and after sort, the column higlighted in flesh colour or pink indicates the columns containing alphanumeric data

    The sort code i use at the moment is as follows with column D being the alphanumeric column

    Sub SortSpecial()
    Dim FirstRow As Long, EndRow As Long, LastRow As Long
    ActiveSheet.Protect Password:="gideon", UserInterfaceOnly:=True
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
    FirstRow = Range("D1").End(xlDown).Row
    
    
    
        Do
            If FirstRow > LastRow Then Exit Sub
            
            EndRow = Range("D" & FirstRow).End(xlDown).Row
            Range("B" & FirstRow, "P" & EndRow).Sort Key1:=Range("D" & FirstRow), Order1:=xlAscending, _
                Key2:=Range("E" & FirstRow), Order2:=xlAscending, _
                Key3:=Range("H" & FirstRow), Order3:=xlAscending, _
                Header:=xlGuess, OrderCustom:=1, _
                MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
                DataOption2:=xlSortNormal
            FirstRow = Range("D" & EndRow).End(xlDown).Row
        Loop
        
     
        
    End Sub
    As you will see from the small example, there are blank cells in the columns and there is no limit to the amount of data that can be contained on the sheet. All other columns that are sorted by teh VB code work as they should

    Hope you can help

    G
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-06-2013
    Location
    Mumbai
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Sorting Alphanumeric Data

    Hey did you find the solution to your problem?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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