Return first word from a string

This tutorial shows how to return the first word from a string using an Excel formula or VBA

Example: Return first word from a string

Return first word from a string

METHOD 1. Return first word from a string

EXCEL

=IFERROR(LEFT(B5,FIND(" ",B5)-1),B5)
This formula uses a combination of the Excel IFERROR, LEFT and FIND functions to return the first word in a string. The FIND function is used to return the position of the first occurrence of a space, which is identified by " ", in the string. The position (as a number) that is returned by the FIND function, minus one, is then applied in the LEFT function as a within_text parameter.
The LEFT function then returns the number of characters that is determined by the position of the first space minus one from the left of the string.

If a string contains only one word the formula, without the IFERROR function, will return an error. Therefore, using the IFERROR function and assigning the original text we ensure that if a string contains only one word the formula will return that word.

METHOD 1. Return first word from a single string using VBA

VBA

Sub Return_first_word_from_string()
'declare variables
Dim ws As Worksheet
Dim val As String
Set ws = Worksheets("Analysis")
val = ws.Range("B5")
On Error Resume Next
'return the first word from a string
ws.Range("C5") = Split(val, " ")(0)

End Sub

ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("C5") in the VBA code.
String: Select the string from which you want extract the first word by changing the cell reference ("B5") in the VBA code.
Worksheet Selection: Select the worksheet which captures the string from which you want to extract the first word by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.

METHOD 2. Return first word from a range using VBA

VBA

Sub Return_first_word_from_string()
'declare variables
Dim ws As Worksheet
Dim firstwrd As String
Set ws = Worksheets("Analysis")
On Error Resume Next
'return the first word from a range of strings
For x = 5 To 10
ws.Range("C" & x) = Split(ws.Range("B" & x), " ")(0)
Next x

End Sub

ADJUSTABLE PARAMETERS
Output Range: Select the output column range by changing the column reference ("C") and the row range by changing the row reference which are the values that are assigned x in the VBA code.
Range: Select the range from which you want to return the first word from each cell by changing the column and row references in the VBA code. The column reference is represented by letter "B" and the row reference is represented by the values that are assigned to x which are 5 and 10. Therefore, the range that this code will loop through will be ("B5:B10"). The row numbers are represented by the "x" value which can also be changed.
Worksheet Selection: Select the worksheet which captures the range from which you want to extract the first word by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.

Explanation about how to return first word from a string

EXPLANATION

EXPLANATION

This tutorial explains how to return the first word from a string using an Excel formula and VBA.
The Excel method uses a combination of the Excel IFERROR, LEFT and FIND functions to return the first word in a string. The FIND function, combined with the (" ") criteria returns the position of the first occurrence of a space (as a number), from which a number one is removed to return the position of the last character of the first word in a cell. This position is then applied inside the LEFT function to return the first word in a string. If there is only one word in the cell, this formula will return an error. Therefore, using the IFERROR function and applying the formula inside it we ensure that if the cell only contains one word, that word is returned instead of an error.
Both of the VBA methods make use of the Split function, with the (" ") criteria to return the first word from a string. The first method shows how the VBA code can be applied if you are only looking to return the first word from a single cell. The second VBA method can be applied if you want to return the first word from multiple cells, where the VBA code will loop through each cell in the specified range and return the first word from each cell.
FORMULA
=IFERROR(LEFT(string,FIND(" ",string)-1),string)
ARGUMENTS
string: The string from which you want to retrieve the first word.

RELATED TOPICS

Related Topic Description Related Topic and Description
How to return the last word from a string using Excel or VBA
How to return the most frequently occurring text using Excel or VBA
How to return the last numeric value in a column using Excel or VBA
How to return the last numeric value in a row using Excel or VBA

RELATED FUNCTIONS

Related Functions Description Related Functions and Description
The Excel IFERROR function returns a specified value if the first value in the IFERROR formula returns an error, otherwise it will return the standard result
The Excel LEFT function returns the specified number of characters from a specified string, starting from the left side
The Excel FIND function returns the position of a specific sub-string within a string