Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Sorting while Ignoring Leading Characters.

Sorting while Ignoring Leading Characters

Written by Allen Wyatt (last updated December 6, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

Ritesh has a huge amount of data that he needs to process in Excel. Each line in a worksheet includes a part number, in column A, by which he needs to sort the records. The problem is that Ritesh cannot do a straight sort; the first four characters of the part numbers need to be ignored. Thus, the sorting actually needs to start with the fifth character of the part number.

The easiest way to handle this situation is to create a new column in Excel that consists entirely of the information you want to use in the sort. In this case, you could add a new, blank column to the right of column A, and then enter this formula in the first cell of the column (assumed to be cell B2):

=RIGHT(A2,LEN(A2)-4)

You'll notice that cell B2 now contains the part number, beginning with the fifth character. Copy this formula down as many cells as necessary, and then perform a standard sort based on the contents of column B.

Another way to come up with the new column—if you are using Excel 2013 or later—is to use the Flash Fill feature. Personally, I find this a bit more restrictive than using the formula already mentioned, but it may work just fine for your needs. All you need to do in cell B2 is to type the portion of cell A2 that you want to use. For instance, if cell A2 contains the part number "ABCDU643," in cell B2 you would type "U643". Then, with cell B2 selected, right-click the fill handle and drag it downward for as many cells as necessary. When you release the mouse button, you'll see a Context menu appear from which you can choose Flash Fill.

I mentioned that I find Flash Fill a bit restrictive because it doesn't always make the proper determination of how the cells should be derived from the originals. For instance, let's say that cell A2 contains "ABCD1234" and that you type "1234" into cell B2. Further, cell A3 contains "EFGHA567". When you Flash Fill based on cell B2, then Excel makes the determination that you want to remove all the letters from the beginning of the other cells, so that B3 ends up containing "567" instead of "A567", as you really wanted. This misinterpretation on the part of Excel only occurs if the first cell (B2) contains only digits.

Regardless of how you get the significant portion of your part numbers into column B, when you perform the sort based on the contents of column B, you'll end up with your part numbers in the desired order. You can then either delete column B, or you can hide it if you may need it later but don't want it to show up.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9870) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Sorting while Ignoring Leading Characters.

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

Understanding the LET Function

The LET function provides an easy way to define and use variables within a formula. This tip shows you how you can start ...

Discover More

Controlling the Automatic Copying of Formulas

When you add a new row of data to the bottom of the data of a worksheet, Excel may (or may not) copy formulas downward to ...

Discover More

Setting Data Validation Input Messages

When using data validation, you might want to have Excel display a message when someone starts to enter information into ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

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

Sorting an Entire List

Need to sort all the data in a table? Here's the fastest and easiest way to do it.

Discover More

Sorting Dates by Month

Sorting by dates is easy, and you end up with a list that is in chronological order. However, things become a bit more ...

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 one less than 9?

2023-12-08 14:04:05

J. Woolley

The TrimHead function would be useful in this case. It is described in my comment here: https://excelribbon.tips.net/T009746_Replacing_Characters_at_the_End_of_a_Cell.html


2023-12-07 05:53:08

Peter

I think that you could you use power query to generate a sorted list in another tab. Something like split column A, sort on column A.2, combine A.1 and A.2


2023-12-06 10:52:11

Don Small

In the interest of the KISS principle (Keep It Sweet & Simple), instead of =RIGHT(A2,LEN(A2)-4), I recommend =MID(A2,4,999).


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.