Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Non-standard Sorting.

Non-standard Sorting

Written by Allen Wyatt (last updated August 24, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003


It is not unusual in an office environment to work with Excel files created by other people. Some of these files can be pretty different than the files you might create. For instance, you might inherit a file in which the first column contains a person's first name on the first line, then their last name on the second line. (The user pressed Alt+Enter to separate the first name from the second name within the same cell.) What if you need to sort the rows in the worksheet based on the last name of the person?

Perhaps the best way to complete such a task is to insert a new column in the worksheet—column B. (This column could be hidden so it doesn't show up when normally working with the worksheet or when printing it out.) The following formula should then be placed in each cell of column B:

=RIGHT(A2,LEN(A2)-FIND(CHAR(10),A2))

Obviously, the cell references will change when placed in column B. In this formula the FIND portion determines the position of the Alt+Enter character (the character code of this character is 10). The RIGHT function returns the characters in the cell starting at the character following the Alt+Enter character. This solution results in column B containing the information on the second line of the first column. You can then easily sort based on the information in column B.

There is one assumption made in this solution—that there are only two lines in each cell of column A. If there are more, or less, then the solution becomes more difficult. If that is the case, the best (and easiest) solution may be to reformat the worksheet so that the sort key is in a column all by itself. If that is not possible (for whatever reason), then the following user-defined VBA function can be used:

Function SecLine(x) As String
    Dim B1 As Integer
    Dim B2 As Integer
    
    B1 = InStr(x, Chr(10))
    B2 = InStr(B1 + 1, x, Chr(10))
    If (B1 + B2) > 0 Then
        If B2 > 0 Then
            SecLine = Mid(x, B1 + 1, B2 – B1 - 1)
        Else
            SecLine = Mid(x, B1 + 1)
        End If
    End If
End Function

To use this routine, simply include the following in the cells in column B:

=SecLine(A2)

Regardless of how many lines there are in cell A2 (in this instance), the function returns a string representing the value of the second line.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2252) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Non-standard Sorting.

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

Hiding and Displaying Hidden Text

You can easily hide text by simply changing the attributes associated with the text. Once that is done, you can turn the ...

Discover More

Using Chart Titles

Titles can be a great addition to any chart. They help provide explanatory information about the information in the ...

Discover More

Summing Every Fourth Cell in a Row

Need to sum a series of cells that fits some regular pattern? Here are several ways that you can get the summation that ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Creating a Sort Order

Excel is very flexible in how it can sort your data. You can even create your own custom sort order that is helpful when ...

Discover More

Sorting for a Walking Tour

Want to sort addresses by even and odd numbers? By using a formula and doing a little sorting, Excel can return the ...

Discover More

Performing Complex Sorts

One way you can easily work with data in a worksheet is to sort it into whatever order you find most helpful. Excel ...

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?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.