Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and 2021. 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 2021


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

Pasting a Comment into Your Document

When developing a document, you may end up with all sorts of comments that you need to deal with. One common task is to ...

Discover More

Using Seek In a Macro

When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...

Discover More

Easily Copying Cell Formatting

Copying table cell formatting from one place to another can be a tedious process as Word doesn't provide a way to do the ...

Discover More

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!

More ExcelTips (ribbon)

Sorting Letters and Numbers

Sorting information in a worksheet can be confusing when Excel applies sorting rules of which you are unaware. This is ...

Discover More

Sorting IP Addresses

If you keep track of IP addresses in an Excel worksheet, you probably already know that it is difficult to sort those ...

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
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 3 + 2?

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.