Sorting Serial Numbers by Their Trailing Characters

Written by Allen Wyatt (last updated August 9, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


4

Vitalis has a column that contains serial numbers. These are formatted such as SFP/0002/2015, SFP/0234/2017, etc., where the final four digits are a year. He needs to sort these serial numbers by year, then by the leading characters. He wonders about the easiest way to do this.

The tried-and-true traditional way to approach this task is to use a helper column to contain what you want as your primary sort key. For instance, let's say that your serial numbers are in column A, with the first one in cell A2. You could place the following formula in cell B2:

=RIGHT(A2,4)

What you end up with is just the year in cell B2. Copy this formula down as many rows as necessary, and then do a traditional sort using the Sort tool on the Data tab of the ribbon. You will want the primary sort to be based on column B and the secondary sort to be based on column A. You can then hide the helper column if you don't want it visible or delete it completely if you won't need to do any additional sorting.

If you are using Office 365, there are some new capabilities that were added to Excel that you may find helpful when it comes to sorting. One of the new functions added (SORTBY) allows you to sort a range of cells quite easily, without the need to use the actual Sort tool available on the Data tab of the ribbon.

Let's say, for instance, that your serial numbers are in the range A2:A1000. You could use the following formula to sort them:

=SORTBY(A2:A1000,RIGHT(A2:A1000,4)&LEFT(A2:A1000,8))

Use this formula, again, in a helper column, and the column will contain the sorted values from A2:A1000. The first parameter supplied to the SORTBY function is the range of cells you want to sort, and the second is the range by which you want them sorted. In this case, that second parameter is an array of values that puts the year first followed by the leading characters of each serial number.

One side benefit of using SORTBY is that your original serial numbers are not positionally changed at all. The helper column contains the sorted serial numbers, so you end up with both lists—sorted and unsorted—in your worksheet.

Remember, again, that the SORTBY function is available only in the version of Excel provided with Office 365. If you will be sharing your workbook with someone using an earlier version of the program, then you'll want to either remove the SORTBY function (by using Paste Values on the sorted values in the helper column) or just rely on the traditional approach discussed earlier.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7971) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing without Headings

One way to use heading styles is to create a story outline. When it comes time to print the story, though, you may not ...

Discover More

Copying a Chart and Related Shapes to a Word Document

Excel and Word are intended to work together, but sometimes it can seem that getting them to do so isn't that intuitive. ...

Discover More

Displaying a Hidden First Row

If you hide the first rows of a worksheet, you may have a hard time getting those rows visible again. Here's a simple way ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More ExcelTips (ribbon)

Storing Sorting Criteria

Need to do the same sorting operation over and over again? Excel doesn't provide a way to save your sorting criteria, but ...

Discover More

Fixing Odd Sorting Behavior

When you sort data that contains both numbers and text, you may not get exactly the result that you expected. To know ...

Discover More

Forcing Excel to Sort Cells as Text

If you have a mixture of numbers and text in a column and you want to sort based upon that column, the results may not be ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 1?

2020-09-15 05:48:59

Harold

Thanks David


2020-09-14 04:06:22

David S

@Harold

It looks like you have forgotten to include the secondary sort that Allen refers to. You need to use 'Custom Sort', set 'Sort by' [year column], and add 'Then By' [Serial number column].


2020-09-12 08:31:17

Ed de Jong

Thanks for pointing out the new SORTBY function.


2020-09-12 06:03:21

Harold Druss

My sorted list by date.
SFP/0225/2017
SFP/0215/2017
SFP/0220/2017

User asked to sort by date, then by leading characters.
I would think the list should look like this?
SFP/0215/2017
SFP/0220/2017
SFP/0225/2017


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.