Sorting by the Last Digits in Variable Length Data

by Allen Wyatt
(last updated October 15, 2020)

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 Office 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

Pasting a Graphic to Multiple Worksheets

Do you need to add a logo or other graphic to a bunch of worksheets? Here are a couple of short macros that can make ...

Discover More

Changing Word to Work in English

What are you to do if you know English but need to work in a version of Word that uses a language other than your own? ...

Discover More

Pulling Filenames into a Worksheet

You can use Excel for all types of data processing. You may want to work with filenames in a worksheet, but the first ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Understanding Ascending and Descending Sorts

When you sort information, Excel follows a set pattern of how your data is organized. This tip illuminates the burning ...

Discover More

Sorting with Graphics

If you use graphics in a worksheet that are associated with certain cells (perhaps images of parts or icons for worksheet ...

Discover More

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

Discover More
Subscribe

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

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}] 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 six minus 3?

2021-01-09 16:29:51

John Mann

What I found interesting was the idea of treating numbers as strings, even when they are formated as numbers and can have arithmetical operations performed on them. That is something I would not have expected - so I've learned something new (to me). BRAVO

2020-10-15 12:21:25

Fred

Sorting by the Last Digits in Variable Length Data

Here's a formula that will achieve the same result:

In column B5 ..... =(A5/100-INT(A5/100))*100

Then sort A and B on column B

It worked on text as well as numeric

Fred

2015-06-22 10:14:23

Ed Phillips

If there is the chance that the data might have less than four digits, you could simply use the "TEXT" function to convert them.

In the examples, simply replace each occurrence of "A1" with the following: TEXT(A1,"0000")
(Formula shown on a separate line to avoid confusion, since it involves the use of double quotes)
This formula will add leading zeros to any value under a thousand.

Important note: this formula will also round any data that is not a whole number. It is possible that the values being imported are displayed as whole numbers when they are not. This may be why Howard had his original problem. A value may be imported as 123456.8 but appear on the spreadsheet as 123457 The "LEFT" function would be evaluate the imported value, rather than the apparent one. so "LEFT(cell,2)" would return ".8" when "57" was expected.

2015-06-21 11:53:00

Bijan

Hi
It was nice and I learned new ability or better I say I learn new techniques of excel
Thanks

2015-06-20 05:30:22

Mudassar Aftab

Hi,

Can you help me with this?

I need to lock a particular cell or range with a password after a particular date has passed. e.g I am collecting Sales Reports from a number of people and who sometimes change their sales in previous dates to reconcile their floor stock. Can there be something to stop them from editing sales data in previous dates.

Regards

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.