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: Extracting First and Last Words.
by Allen Wyatt
(last updated June 25, 2016)
Reggie has a cell that contains three or more words. (The number of words could vary.) He needs a formula that allows him to extract either the first word of the cell or the last word of the cell. For instance, if the cell contains the phrase "Reggie was here in 2016", then he needs a formula to extract "Reggie" and one to extract "2016".
You can extract both words using formulas. Extracting the first word is relatively straightforward. All you need to do is find the location of the first space in the phrase, then extract whatever is to the left of it. If one presumes that the phrase is in A1, one can use the formula:
To extract the last word, you'll need a slightly different formula:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A1)," ",REPT(" ",255)),255))
This formula changes the spaces into strings of 255 blanks. Then it finds the last 255 characters and trims the characters to the left, leaving the last word.
You can also, if you prefer, create user-defined functions to grab the words you want. Grabbing the first word is easy:
Function FirstWord(c As String) Dim arr arr = Split(Trim(c), " ") FirstWord = arr(LBound(arr)) End Function
The function uses the Split function to pull apart whatever is in the specified cell, using the second parameter (" ") as the delimiter. Each element in the array (arr) then contains a portion of the original string. In this case what is being returned is the first element (specified by LBound) of the array—the first word.
Since the words from the phrase are being placed in an array, you can use just a slight variation on the function to return the last word:
Function LastWord(c As String) Dim arr arr = Split(Trim(c), " ") LastWord = arr(UBound(arr)) End Function
Note that, essentially, the only real change in the function is the use of UBound instead of LBound. The UBound function specifies the last element of the array. You can use both of these functions in a worksheet in this manner:
If you prefer, you could bypass using the Split function and, instead, use some other string-related functions:
Function GetFirst(c As String) GetFirst = Left(c, InStr(c, " ") - 1) End Function
Function GetLast(c As String) GetFirst = Mid(c, InstrRev(c, " ") + 1) End Function
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11985) 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: Extracting First and Last Words.
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!
Excel allows you to enter two different types of formulas in a cell: A regular formula or an array formula. If you need to ...Discover More
If you have a list of data in a column, you may want to determine an average of whatever the last few items are in the ...Discover More
If you've got a list of names in a column, you may want to change the order of each name. For instance, the name have the ...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.