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

Worksheet Events

You can create macros that are automatically executed whenever certain events occur within a worksheet. This tip details ...

Discover More

Creating Long Page Footers

Ever wish that you could create nice, long footers that appear at the bottom of each page when you print your worksheet? ...

Discover More

Filling Table Cells with a Macro

Want to stuff a value into each cell of a table? You can either type the value over and over and over again, or you can ...

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)

Incomplete and Corrupt Sorting

Using the sorting tools, on the toolbar, may result in some unwanted results, such as jumbled data. If this happens to ...

Discover More

Separating Cells Based on Text Color

If the font color used for the data in your worksheet is critical, you may at some time want to move cells that use a ...

Discover More

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
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 8 - 5?

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.