Solved

Developing and automating a custom alphanumeric sort in Excel

Posted on 2014-07-25
13
270 Views
Last Modified: 2014-07-28
I need to be able to develop and automate a custom sort on an alphanumeric list. We will be sorting this type of data on a regular basis, hence the most automated approach would be best. I have attached a sample excel showing the Excel default sort in column A, and the correct custom sort I need in column B.

Thanks,
Andrea
Sample-alphanumeric-sort.xlsx
0
Comment
Question by:Andreamary
13 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40219633
Correct me if my observation is wrong. This is the sort you need:
1. The data should be separated into subsets for each number of characters per word
2.  Each subset should be sorted by itself.
3. Once the data is separated into subsets it actually is sorted already.
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 40219697
If MacroShadow is correct, to achieve that sort use a helper cell alongside the data with the following formula:

=LEN(A2)

Sort on the column contianing this formula and then on column A.

Thanks
Rob H
0
 

Author Comment

by:Andreamary
ID: 40219778
I tried the above formula but it wasn't entirely successful...I've attached my results and highlighted the errors in red. They pertain to the "N" section, with those identified as "4's" in the helper column. To answer Macroshadow to the best of my abilities:

First sort — alpha, on first letter in string
Second sort — numeric, on all numbers within string, treating the number sequence within the string as a single number (it can be 1, 2, 3+ digits as per the example).
Third sort — alpha, on last letter in string

Thanks!
Andrea
Alphanumeric-HelperColumn.xlsx
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40219827
No points..just clarifying how you should sort using Rob's helper column:
sort order
-Glenn
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40219870
Followup:  Where would the value "CUT16" be ordered?  It does not have a single letter to start, but based on your rules above, I presume it should be at the top of the list.  Are there any other designations like this that might need to be considered?
0
 

Author Comment

by:Andreamary
ID: 40219907
Hi Glenn,
Thanks for noting the CUT exception...I'm not very familiar with the data (troubleshooting for a colleague) but you're right, CUT should be at the top of the list. And confirming that I did sort as per your example with the helper column.

Andrea
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40219946
So, given that issue, would it be correct to re-state the sorting algorithm like so?

1) Sort on all alphabetic characters at the start of each string, ascending
2) Sort on all numeric characters that follow, ascending
3) Sort on any alphabetic characters at the end of the string (if they exist), ascending
0
 

Author Comment

by:Andreamary
ID: 40219964
Yes, a formula (or VBA) that does that should do the trick...

Thanks,
Andrea
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40220323
As I was writing a UDF (user-defined function) that would help supply a new sorting string, I noticed that there were no instances where there was more than one letter suffix for any given set of letter-number combinations.  For example:
N104A
N104D

N300A
N300C

Put another way, instead of the designations being constructed like this:
AlphaCode1 - NumericCode (- AlphaCode2)   <---last value optional
it's more like:
AlphaCode1 - AlphanumericCode

Ex:
N-300-A
N-300A

meaning that the alphanumeric group is mutually-exclusive.

Long story short, I wanted to make sure that this would work if the first case was true, so I added some extra test values (in green) to ensure the UDF would work.  The attached workbook contains this function:
Function SortString(strInput As String)
    Dim x As Integer
    Dim strNum As String
    Dim boolBreak As Boolean
    For x = 1 To Len(strInput)
        If IsNumeric(Mid(strInput, x, 1)) Then
            boolBreak = True
            Do
                strNum = strNum & Mid(strInput, x, 1)
                x = x + 1
            Loop Until IsNumeric(Mid(strInput, x, 1)) = False
            SortString = SortString & Format(Val(strNum), "000000")
        Else
            SortString = SortString & Mid(strInput, x, 1)
        End If
    Next x
End Function

Open in new window


You would sort on the helper column with the function (col. B), then the Designation column to ensure that extra letter combinations are picked up correctly.

Regards,
-Glenn
EE-Alphanumeric-HelperColumn.xlsm
0
 

Author Comment

by:Andreamary
ID: 40222676
Hi Glenn,

Thanks very much. I've tested this out, and it appears that when I sort by Column B (SortString), then by column A, there are still issues with the numbers being out of order (3-digit numbers preceding 2-digit numbers as shown below). It appears, though, if I change the order in which I sort the columns, sorting Column A first, then Column B, it appears to work. Is there a reason it would work this way, and would it always work? I've attached my test SortString file.

N388A
N389A
N38F

N419A
N41D
N420A
AlphaNumeric-SortString-Test.xlsm
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40223453
When you do the sort, it should show the column names and you should select as follows:
correct sorting order
This is the only way it should work.

-Glenn
0
 

Author Closing Comment

by:Andreamary
ID: 40224384
It works perfectly, Glenn...much appreciated! (Thanks for your patience in steering me in the right direction..I was manually sorting each column instead of using the SORT interface as shown in your screen capture.)
Cheers,
Andrea
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40224719
You're welcome.  Wish I could have figured an "elegant" helper formula instead of a UDF, but glad that it works.

-Glenn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now