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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Custom lists are handy ways to enter recurring data in a worksheet. Here's how you can import your own custom lists from ...
Discover MoreGovernment and industrial organizations often use a numbering system that relies upon a number both before and after a ...
Discover MoreWhen you sort data in a worksheet, you don't need to sort everything at once. You can sort just a portion of your data by ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments