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

Extracting First and Last Words

Written by Allen Wyatt (last updated July 30, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

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 2022", then he needs a formula to extract "Reggie" and one to extract "2022".

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:

=LEFT(A1,FIND(" ",A1)-1)

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:

=FirstWord(A1)
=LastWord(A1)

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

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 (11985) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Extracting First and Last Words.

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

Flipping Landscape Orientation when Printing

When printing a worksheet, you may want to rotate the output on the page to fit a certain orientation. Excel doesn't ...

Discover More

Print Layout Zoom Settings

The zoom setting at which you view a document can sometimes be saved with a document. This tip explains how the zoom ...

Discover More

Finding and Replacing when Only a Portion of the Text is Superscripted

Finding and replacing text when the text being searched for has mixed formatting can be a challenge. You may, however, ...

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)

Transposing and Linking

Sometimes it is helpful to look at data that is rotated 90 degrees. Excel allows you to use Paste Special to transpose ...

Discover More

Finding Odd Values Greater Than 50

If you have a special need to find cell values that meet two different criteria, where to start can be daunting. This tip ...

Discover More

Counting Precedents and Dependents

Do you need to know how many precedents or dependents there are on a worksheet? You could count them manually, or you ...

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 more than 7?

2022-08-01 01:36:00

Shadeburst

Yes I know that a cell can hold 32,767 characters but isn't 255 overkill? :)

Flash fill is so powerful at recognizing patterns that usually it will be the optimum solution. Sometimes one or two helper columns may be needed to train Flash Fill.


2022-07-30 11:47:34

J. Woolley

For related discussion, see the following Tip's comments: https://excelribbon.tips.net/T011399_Reversing_Names_In_Place.html


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.