Sorting by the Last Digits in Variable Length Data

Written by Allen Wyatt (last updated August 14, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Howard has a list of numbers, sometimes 6 digits, sometimes 8 digits, but the length is irrelevant as he needs to sort by the last 2 digits and then by the previous 2 digits. He tried right/middle/left types of formulas, but with having varying numbers of digits he could not make it work.

Before discussing approaches, it is important to make a few explicit assumptions about Howard's data. In this tip I'll assume that the data is in column A and that it consists of at least 4 digits. (Howard mentions lengths of 6 and 8 digits, so this shouldn't be a false assumption.) It is also assumed that the only thing in column A are values made up of digits—it doesn't contain other characters that you want ignored in your sorting.

If you wanted to use two helper columns to extract the sorting digits, then you could use the following in column B:

=RIGHT(A1,2)

The formula grabs the two right-most digits from whatever is in column A. The following could then be placed into column C:

=MID(A1, LEN(A1)-3,2)

This formula looks at the length of whatever is in A1 and uses that value as a parameter for the MID function to extract the two digits that precede the final two digits in the value.

It is important to realize that these formulas return text values, regardless of whether the values in column A are numeric or text. This is handy when either of the extracted values begin with 0; it means that instead of "5" being returned you'll see "05" or "00" instead of "0". If you want to make sure that numeric values are returned, then you'll need to wrap each formula in the VALUE function:

=VALUE(RIGHT(A1,2))
=VALUE(MID(A1, LEN(A1)-3,2))

With your "key values" in columns B and C you could then perform your sort based on those values. You could also, if desired, use only a single helper column with either of these formulas in column B:

=RIGHT(A1,2)&MID(A1, LEN(A1)-3,2)
=RIGHT(A1,2)&LEFT(RIGHT(A1,4),2)

Again, these return text strings (which are just fine for sorting), but you could convert them to numeric values by wrapping them in the VALUE function:

=VALUE(RIGHT(A1,2)&MID(A1, LEN(A1)-3,2))
=VALUE(RIGHT(A1,2)&LEFT(RIGHT(A1,4),2))

Regardless of whether you work with extracted text or a numeric value in column B, you can now sort based on the column contents.

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

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

Word Slow to Open Documents

If you've noticed a slowdown in Word when it is opening a document, you probably would like to speed up the operation. ...

Discover More

Factory Default Settings for Word

Do you long for a way to reset Word to a "factory default" condition? It is almost impossible to get things to the way ...

Discover More

Displaying Quick Document Statistics

Need to see how many pages, words, paragraphs, or lines are in your document? Word makes it easy to retrieve such ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Sorting for a Walking Tour

Want to sort addresses by even and odd numbers? By using a formula and doing a little sorting, Excel can return the ...

Discover More

Sorting IP Addresses

If you keep track of IP addresses in an Excel worksheet, you probably already know that it is difficult to sort those ...

Discover More

Undoing the Effects of a Sort

Have you ever put a lot of data into a worksheet, sorted that data, and then realized you shouldn't have done so? It is ...

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 two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.