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 2021.
Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!
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 MoreIf you keep track of IP addresses in an Excel worksheet, you probably already know that it is difficult to sort those ...
Discover MoreWhen you sort data that contains both numbers and text, you may not get exactly the result that you expected. To know ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2026-08-12 05:27:04
jamies
Rather than mid() and len(),
Start with ensuring the string is at least 4 characters long,
and extract the leading 2 characters from the rightmost 4
so
=right("00"&A1,2)&left(right("0000"&A1,4),2)
Maybe use rept("0",4) for a more general extension,.
It may not be needed for the process if the data is always at least long enough, and is text, and was not created from a negative value, and not from a non integer value.
But - from experience - either ensure the data is as expected, or adjust it to be as appropriate, and ensure it does give the data structured as the code needs it to be
Note - my suggestions do not deal with such as currency signs, or Accounting layouts or such as 2-3 being taken as an arithmetical calculation.
So - always work on the basis that if not adequately constrained a user's input, or Excel conversion assumption, or foreign language, location or indication to excel, or setting in the OS, or the version of VBA , etc. will generate an unexpected, and unwanted result.
And - then there is the prefix that sets excel to take the input as a set of characters
as in the user entered '0123456
or the cell was set to "Text" format,
or data was copied in from a cell set as numeric, currency,
or text data was copied into a cell set as numeric, currency,
so many ways to annoy the unwary user !
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 © 2026 Sharon Parq Associates, Inc.
Comments