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

Getting Audible Feedback

You can add a bit of sound to your editing tasks by turning on Word's sound capabilities. This tip shows where this ...

Discover More

Updating a Field in a Text Box

If you put a field into a text box, you might be surprised to find that it doesn't update when you try to update all your ...

Discover More

Three-Dimensional Transpositions

Excel makes it easy to transpose your data so that rows become columns and columns rows. It doesn't have a built-in ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Importing Custom Lists

Custom lists are handy ways to enter recurring data in a worksheet. Here's how you can import your own custom lists from ...

Discover More

Storing Sorting Criteria

Need to do the same sorting operation over and over again? Excel doesn't provide a way to save your sorting criteria, but ...

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
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 two 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.