Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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: Non-standard Sorting.
by Allen Wyatt
(last updated August 20, 2016)
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:
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:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11925) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Non-standard Sorting.
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!
If you use graphics in a worksheet that are associated with certain cells (perhaps images of parts or icons for worksheet ...Discover More
If you keep track of IP addresses in an Excel worksheet, you probably already know that it is difficult to sort those ...Discover More
When you sort information in a worksheet, you have control over the order in which that information is sorted. Here's a quick ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.