Excel is mostly about numerical, however, we often have data of text data type. Here are a few functions we should know to handle text data.
- LEFT
- RIGHT
- MID
- LEN
- FIND
The LEFT function
The LEFT function returns a given text from the left of our text string based on the number of characters specified.
Syntax:
LEFT(text, [num_chars])
Parameters:
- Text: The text we want to extract from.
- Num_chars (Optional): The number of characters you want to extract. Default num_chars is 1 and the number must be a positive number that is greater than zero.
Example:
Step 1: Format your data.
Now if you want to get the first “Geeks” from “Geeksforgeeks” in B2. Let us follow the next step.
Step 2: We will enter =LEFT(B2,5) in the B3 cell. Here we want Excel to extract the first 5 characters from the left side of our text in B2.
This will return “Geeks”.
The RIGHT Function
The RIGHT function returns a given text from the left of our text string based on the number of characters specified.
Syntax:
RIGHT(text, [num_chars])
Parameters:
- Text: The text we want to extract from.
- Num_chars (Optional): The number of characters you want to extract. Default num_chars is 1 and the number must be a positive number that is greater than zero.
Example:
Step 1: Format your data.
Now if you want to get the last geeks from “Geeksforgeeks” in B2. Let us follow the next step.
Step 2: We will enter =RIGHT(B2,5) in the B3 cell. Here we want Excel to extract the last 5 characters from the right side of our text in B2.
This will return “geeks”.
The MID function
The MID function returns the text from any middle part of our text string based on the starting position and number of characters specified.
Syntax:
MID(text, start_num, num_chars)
Parameters:
- Text: The text we want to extract from.
- start_num: The starting number of the first character from the text we want to extract.
- Num_chars: The number of characters you want to extract.
Example:
Step 1: Format your data.
Now if you want to get the character “for” which is located in the middle of our text “Geeksforgeeks” in B2. Let us follow the next step.
Step 2: We will enter =MID(B2,5,3) in the B3 cell. Here we want Excel to extract the characters located in the middle of our text in B2.
This will return “for”.
The Len Function
The LEN function returns the number of characters in the text strings.
Syntax:
LEN(text)
Parameters:
- Text: The text we want to know the length
Example:
Step 1: Format your data.
Now if you want to know how many characters are in the text “Geeksforgeeks”. Let us follow the next step.
Step 2: We will enter =LEN(B2) in the B3 cell. Excel will count how many characters are in the text.
This will return 13.
The FIND Function
The FIND function returns the position of a given text within a text.
Syntax:
FIND(find_text, within_text, [start_num])
Parameters:
- Find_text: The text we want to find.
- Within_text: The text containing our find_text.
- Start_num (Optional): The starting position of our find_text. Default is 1.
Example:
Step 1: Format your data.
Now if you want to find “for” in geeksforgeeks in B2. Let us follow the next step.
Step 2: We will enter =FIND(“for”,B2) in B3 cell. Here we want Excel to find “for” in our text in B2.
This will return 6 because “for” is located at character number 6 in our text.
Извлечение (вырезание) части строки с помощью кода VBA Excel из значения ячейки или переменной. Функции Left, Mid и Right, их синтаксис и аргументы. Пример.
Эта функция извлекает левую часть строки с заданным количеством символов.
Синтаксис функции Left:
Left(строка, длина)
- строка — обязательный аргумент: строковое выражение, из значения которого вырезается левая часть;
- длина — обязательный аргумент: числовое выражение, указывающее количество извлекаемых символов.
Если аргумент «длина» равен нулю, возвращается пустая строка. Если аргумент «длина» равен или больше длины строки, возвращается строка полностью.
Функция Mid
Эта функция извлекает часть строки с заданным количеством символов, начиная с указанного символа (по номеру).
Синтаксис функции Mid:
Mid(строка, начало, [длина])
- строка — обязательный аргумент: строковое выражение, из значения которого вырезается часть строки;
- начало — обязательный аргумент: числовое выражение, указывающее положение символа в строке, с которого начинается извлекаемая часть;
- длина — необязательный аргумент: числовое выражение, указывающее количество вырезаемых символов.
Если аргумент «начало» больше, чем количество символов в строке, функция Mid возвращает пустую строку. Если аргумент «длина» опущен или его значение превышает количество символов в строке, начиная с начального, возвращаются все символы от начальной позиции до конца строки.
Функция Right
Эта функция извлекает правую часть строки с заданным количеством символов.
Синтаксис функции Right:
Right(строка, длина)
- строка — обязательный аргумент: строковое выражение, из значения которого вырезается правая часть;
- длина — обязательный аргумент: числовое выражение, указывающее количество извлекаемых символов.
Если аргумент «длина» равен нулю, возвращается пустая строка. Если аргумент «длина» равен или больше длины строки, возвращается строка полностью.
Пример
В этом примере будем использовать все три представленные выше функции для извлечения из ФИО его составных частей. Для этого запишем в ячейку «A1» строку «Иванов Сидор Петрович», из которой вырежем отдельные компоненты и запишем их в ячейки «A2:A4».
Sub Primer() Dim n1 As Long, n2 As Long Range(«A1») = «Иванов Сидор Петрович» ‘Определяем позицию первого пробела n1 = InStr(1, Range(«A1»), » «) ‘Определяем позицию второго пробела n2 = InStr(n1 + 1, Range(«A1»), » «) ‘Извлекаем фамилию Range(«A2») = Left(Range(«A1»), n1 — 1) ‘Извлекаем имя Range(«A3») = Mid(Range(«A1»), n1 + 1, n2 — n1 — 1) ‘Извлекаем отчество Range(«A4») = Right(Range(«A1»), Len(Range(«A1»)) — n2) End Sub |
На практике часто встречаются строки с лишними пробелами, которые необходимо удалить перед извлечением отдельных слов.
Split text into different columns with functions
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel Web App Excel 2010 Excel 2007 Excel for Mac 2011 More…Less
You can use the LEFT, MID, RIGHT, SEARCH, and LEN text functions to manipulate strings of text in your data. For example, you can distribute the first, middle, and last names from a single cell into three separate columns.
The key to distributing name components with text functions is the position of each character within a text string. The positions of the spaces within the text string are also important because they indicate the beginning or end of name components in a string.
For example, in a cell that contains only a first and last name, the last name begins after the first instance of a space. Some names in your list may contain a middle name, in which case, the last name begins after the second instance of a space.
This article shows you how to extract various components from a variety of name formats using these handy functions. You can also split text into different columns with the Convert Text to Columns Wizard
Example name |
Description |
First name |
Middle name |
Last name |
Suffix |
|
1 |
Jeff Smith |
No middle name |
Jeff |
Smith |
||
2 |
Eric S. Kurjan |
One middle initial |
Eric |
S. |
Kurjan |
|
3 |
Janaina B. G. Bueno |
Two middle initials |
Janaina |
B. G. |
Bueno |
|
4 |
Kahn, Wendy Beth |
Last name first, with comma |
Wendy |
Beth |
Kahn |
|
5 |
Mary Kay D. Andersen |
Two-part first name |
Mary Kay |
D. |
Andersen |
|
6 |
Paula Barreto de Mattos |
Three-part last name |
Paula |
Barreto de Mattos |
||
7 |
James van Eaton |
Two-part last name |
James |
van Eaton |
||
8 |
Bacon Jr., Dan K. |
Last name and suffix first, with comma |
Dan |
K. |
Bacon |
Jr. |
9 |
Gary Altman III |
With suffix |
Gary |
Altman |
III |
|
10 |
Mr. Ryan Ihrig |
With prefix |
Ryan |
Ihrig |
||
11 |
Julie Taft-Rider |
Hyphenated last name |
Julie |
Taft-Rider |
Note: In the graphics in the following examples, the highlight in the full name shows the character that the matching SEARCH formula is looking for.
This example separates two components: first name and last name. A single space separates the two names.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Jeff Smith |
No middle name |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
-
First name
The first name starts with the first character in the string (J) and ends at the fifth character (the space). The formula returns five characters in cell A2, starting from the left.
Use the SEARCH function to find the value for num_chars:
Search for the numeric position of the space in A2, starting from the left.
-
Last name
The last name starts at the space, five characters from the right, and ends at the last character on the right (h). The formula extracts five characters in A2, starting from the right.
Use the SEARCH and LEN functions to find the value for num_chars:
Search for the numeric position of the space in A2, starting from the left. (5)
-
Count the total length of the text string, and then subtract the number of characters to the left of the first space, as found in step 1.
This example uses a first name, middle initial, and last name. A space separates each name component.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Eric S. Kurjan |
One middle initial |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (middle initial) |
‘=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-SEARCH(» «,A2,1)) |
=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-SEARCH(» «,A2,1)) |
Formula |
Live Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
-
First name
The first name starts with the first character from the left (E) and ends at the fifth character (the first space). The formula extracts the first five characters in A2, starting from the left.
Use the SEARCH function to find the value for num_chars:
Search for the numeric position of the space in A2, starting from the left. (5)
-
Middle name
The middle name starts at the sixth character position (S), and ends at the eighth position (the second space). This formula involves nesting SEARCH functions to find the second instance of a space.
The formula extracts three characters, starting from the sixth position.
Use the SEARCH function to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the first character from the left. (5).
-
Add 1 to get the position of the character after the first space (S). This numeric position is the starting position of the middle name. (5 + 1 = 6)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the first character from the left. (5)
-
Add 1 to get the position of the character after the first space (S). The result is the character number at which you want to start searching for the second instance of space. (5 + 1 = 6)
-
Search for the second instance of space in A2, starting from the sixth position (S) found in step 4. This character number is the ending position of the middle name. (8)
-
Search for the numeric position of space in A2, starting from the first character from the left. (5)
-
Take the character number of the second space found in step 5 and subtract the character number of the first space found in step 6. The result is the number of characters MID extracts from the text string starting at the sixth position found in step 2. (8 – 5 = 3)
-
Last name
The last name starts six characters from the right (K) and ends at the first character from the right (n). This formula involves nesting SEARCH functions to find the second and third instances of a space (which are at the fifth and eighth positions from the left).
The formula extracts six characters in A2, starting from the right.
-
Use the LEN and nested SEARCH functions to find the value for num_chars:
Search for the numeric position of space in A2, starting from the first character from the left. (5)
-
Add 1 to get the position of the character after the first space (S). The result is the character number at which you want to start searching for the second instance of space. (5 + 1 = 6)
-
Search for the second instance of space in A2, starting from the sixth position (S) found in step 2. This character number is the ending position of the middle name. (8)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the second instance of space found in step 3. The result is the number of characters to be extracted from the right of the full name. (14 – 8 = 6).
Here’s an example of how to extract two middle initials. The first and third instances of space separate the name components.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Janaina B. G. Bueno |
Two middle initials |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (middle initials) |
‘=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)-SEARCH(» «,A2,1)) |
=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)-SEARCH(» «,A2,1)) |
Formula |
Live Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
-
First name
The first name starts with the first character from the left (J) and ends at the eighth character (the first space). The formula extracts the first eight characters in A2, starting from the left.
Use the SEARCH function to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (8)
-
Middle name
The middle name starts at the ninth position (B), and ends at the fourteenth position (the third space). This formula involves nesting SEARCH to find the first, second, and third instances of space in the eighth, eleventh, and fourteenth positions.
The formula extracts five characters, starting from the ninth position.
Use the SEARCH function to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the first character from the left. (8)
-
Add 1 to get the position of the character after the first space (B). This numeric position is the starting position of the middle name. (8 + 1 = 9)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the first character from the left. (8)
-
Add 1 to get the position of the character after the first space (B). The result is the character number at which you want to start searching for the second instance of space. (8 + 1 = 9)
-
Search for the second space in A2, starting from the ninth position (B) found in step 4. (11).
-
Add 1 to get the position of the character after the second space (G). This character number is the starting position at which you want to start searching for the third space. (11 + 1 = 12)
-
Search for the third space in A2, starting at the twelfth position found in step 6. (14)
-
Search for the numeric position of the first space in A2. (8)
-
Take the character number of the third space found in step 7 and subtract the character number of the first space found in step 6. The result is the number of characters MID extracts from the text string starting at the ninth position found in step 2.
-
Last name
The last name starts five characters from the right (B) and ends at the first character from the right (o). This formula involves nesting SEARCH to find the first, second, and third instances of space.
The formula extracts five characters in A2, starting from the right of the full name.
Use nested SEARCH and the LEN functions to find the value for the num_chars:
Search for the numeric position of the first space in A2, starting from the first character from the left. (8)
-
Add 1 to get the position of the character after the first space (B). The result is the character number at which you want to start searching for the second instance of space. (8 + 1 = 9)
-
Search for the second space in A2, starting from the ninth position (B) found in step 2. (11)
-
Add 1 to get the position of the character after the second space (G). This character number is the starting position at which you want to start searching for the third instance of space. (11 + 1 = 12)
-
Search for the third space in A2, starting at the twelfth position (G) found in step 6. (14)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the third space found in step 5. The result is the number of characters to be extracted from the right of the full name. (19 — 14 = 5)
In this example, the last name comes before the first, and the middle name appears at the end. The comma marks the end of the last name, and a space separates each name component.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Kahn, Wendy Beth |
Last name first, with comma |
Formula |
Result (first name) |
‘=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-SEARCH(» «,A2,1)) |
=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-SEARCH(» «,A2,1)) |
Formula |
Result (middle name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
Formula |
Live Result (last name) |
‘=LEFT(A2, SEARCH(» «,A2,1)-2) |
=LEFT(A2, SEARCH(» «,A2,1)-2) |
-
First name
The first name starts with the seventh character from the left (W) and ends at the twelfth character (the second space). Because the first name occurs at the middle of the full name, you need to use the MID function to extract the first name.
The formula extracts six characters, starting from the seventh position.
Use the SEARCH function to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the first character from the left. (6)
-
Add 1 to get the position of the character after the first space (W). This numeric position is the starting position of the first name. (6 + 1 = 7)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the first character from the left. (6)
-
Add 1 to get the position of the character after the first space (W). The result is the character number at which you want to start searching for the second space. (6 + 1 = 7)
Search for the second space in A2, starting from the seventh position (W) found in step 4. (12)
-
Search for the numeric position of the first space in A2, starting from the first character from the left. (6)
-
Take the character number of the second space found in step 5 and subtract the character number of the first space found in step 6. The result is the number of characters MID extracts from the text string starting at the seventh position found in step 2. (12 — 6 = 6)
-
Middle name
The middle name starts four characters from the right (B), and ends at the first character from the right (h). This formula involves nesting SEARCH to find the first and second instances of space in the sixth and twelfth positions from the left.
The formula extracts four characters, starting from the right.
Use nested SEARCH and the LEN functions to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the first character from the left. (6)
-
Add 1 to get the position of the character after the first space (W). The result is the character number at which you want to start searching for the second space. (6 + 1 = 7)
-
Search for the second instance of space in A2 starting from the seventh position (W) found in step 2. (12)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the second space found in step 3. The result is the number of characters to be extracted from the right of the full name. (16 — 12 = 4)
-
Last name
The last name starts with the first character from the left (K) and ends at the fourth character (n). The formula extracts four characters, starting from the left.
Use the SEARCH function to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the first character from the left. (6)
-
Subtract 2 to get the numeric position of the ending character of the last name (n). The result is the number of characters you want LEFT to extract. (6 — 2 =4)
This example uses a two-part first name, Mary Kay. The second and third spaces separate each name component.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Mary Kay D. Andersen |
Two-part first name |
Formula |
Result (first name) |
LEFT(A2, SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
=LEFT(A2, SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
Formula |
Result (middle initial) |
‘=MID(A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1,SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)-(SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
=MID(A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1,SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)-(SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
Formula |
Live Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
-
First name
The first name starts with the first character from the left and ends at the ninth character (the second space). This formula involves nesting SEARCH to find the second instance of space from the left.
The formula extracts nine characters, starting from the left.
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the first character from the left. (5)
-
Add 1 to get the position of the character after the first space (K). The result is the character number at which you want to start searching for the second instance of space. (5 + 1 = 6)
-
Search for the second instance of space in A2, starting from the sixth position (K) found in step 2. The result is the number of characters LEFT extracts from the text string. (9)
-
Middle name
The middle name starts at the tenth position (D), and ends at the twelfth position (the third space). This formula involves nesting SEARCH to find the first, second, and third instances of space.
The formula extracts two characters from the middle, starting from the tenth position.
Use nested SEARCH functions to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the first character from the left. (5)
-
Add 1 to get the character after the first space (K). The result is the character number at which you want to start searching for the second space. (5 + 1 = 6)
-
Search for the position of the second instance of space in A2, starting from the sixth position (K) found in step 2. The result is the number of characters LEFT extracts from the left. (9)
-
Add 1 to get the character after the second space (D). The result is the starting position of the middle name. (9 + 1 = 10)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the character after the second space (D). The result is the character number at which you want to start searching for the third space. (10)
-
Search for the numeric position of the third space in A2, starting from the left. The result is the ending position of the middle name. (12)
-
Search for the numeric position of the character after the second space (D). The result is the beginning position of the middle name. (10)
-
Take the character number of the third space, found in step 6, and subtract the character number of “D”, found in step 7. The result is the number of characters MID extracts from the text string starting at the tenth position found in step 4. (12 — 10 = 2)
-
Last name
The last name starts eight characters from the right. This formula involves nesting SEARCH to find the first, second, and third instances of space in the fifth, ninth, and twelfth positions.
The formula extracts eight characters from the right.
Use nested SEARCH and the LEN functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (5)
-
Add 1 to get the character after the first space (K). The result is the character number at which you want to start searching for the space. (5 + 1 = 6)
-
Search for the second space in A2, starting from the sixth position (K) found in step 2. (9)
-
Add 1 to get the position of the character after the second space (D). The result is the starting position of the middle name. (9 + 1 = 10)
-
Search for the numeric position of the third space in A2, starting from the left. The result is the ending position of the middle name. (12)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the third space found in step 5. The result is the number of characters to be extracted from the right of the full name. (20 — 12 =
This example uses a three-part last name: Barreto de Mattos. The first space marks the end of the first name and the beginning of the last name.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Paula Barreto de Mattos |
Three-part last name |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (last name) |
RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
-
First name
The first name starts with the first character from the left (P) and ends at the sixth character (the first space). The formula extracts six characters from the left.
Use the Search function to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Last name
The last name starts seventeen characters from the right (B) and ends with first character from the right (s). The formula extracts seventeen characters from the right.
Use the LEN and SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the first space, found in step 1. The result is the number of characters to be extracted from the right of the full name. (23 — 6 = 17)
This example uses a two-part last name: van Eaton. The first space marks the end of the first name and the beginning of the last name.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
James van Eaton |
Two-part last name |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
-
First name
The first name starts with the first character from the left (J) and ends at the eighth character (the first space). The formula extracts six characters from the left.
Use the SEARCH function to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Last name
The last name starts with the ninth character from the right (v) and ends at the first character from the right (n). The formula extracts nine characters from the right of the full name.
Use the LEN and SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the first space, found in step 1. The result is the number of characters to be extracted from the right of the full name. (15 — 6 = 9)
In this example, the last name comes first, followed by the suffix. The comma separates the last name and suffix from the first name and middle initial.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Bacon Jr., Dan K. |
Last name and suffix first, with comma |
Formula |
Result (first name) |
‘=MID(A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1,SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
=MID(A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1,SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
Formula |
Result (middle initial) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)+1)) |
Formula |
Result (last name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (suffix) |
‘=MID(A2,SEARCH(» «, A2,1)+1,(SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-2)-SEARCH(» «,A2,1)) |
=MID(A2,SEARCH(» «, A2,1)+1,(SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-2)-SEARCH(» «,A2,1)) |
-
First name
The first name starts with the twelfth character (D) and ends with the fifteenth character (the third space). The formula extracts three characters, starting from the twelfth position.
Use nested SEARCH functions to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Add 1 to get the character after the first space (J). The result is the character number at which you want to start searching for the second space. (6 + 1 = 7)
-
Search for the second space in A2, starting from the seventh position (J), found in step 2. (11)
-
Add 1 to get the character after the second space (D). The result is the starting position of the first name. (11 + 1 = 12)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the character after the second space (D). The result is the character number at which you want to start searching for the third space. (12)
-
Search for the numeric position of the third space in A2, starting from the left. The result is the ending position of the first name. (15)
-
Search for the numeric position of the character after the second space (D). The result is the beginning position of the first name. (12)
-
Take the character number of the third space, found in step 6, and subtract the character number of “D”, found in step 7. The result is the number of characters MID extracts from the text string starting at the twelfth position, found in step 4. (15 — 12 = 3)
-
Middle name
The middle name starts with the second character from the right (K). The formula extracts two characters from the right.
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Add 1 to get the character after the first space (J). The result is the character number at which you want to start searching for the second space. (6 + 1 = 7)
-
Search for the second space in A2, starting from the seventh position (J), found in step 2. (11)
-
Add 1 to get the character after the second space (D). The result is the starting position of the first name. (11 + 1 = 12)
-
Search for the numeric position of the third space in A2, starting from the left. The result is the ending position of the middle name. (15)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the third space, found in step 5. The result is the number of characters to be extracted from the right of the full name. (17 — 15 = 2)
-
Last name
The last name starts at the first character from the left (B) and ends at sixth character (the first space). Therefore, the formula extracts six characters from the left.
Use the SEARCH function to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Suffix
The suffix starts at the seventh character from the left (J), and ends at ninth character from the left (.). The formula extracts three characters, starting from the seventh character.
Use the SEARCH function to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Add 1 to get the character after the first space (J). The result is the starting position of the suffix. (6 + 1 = 7)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Add 1 to get the numeric position of the character after the first space (J). The result is the character number at which you want to start searching for the second space. (7)
-
Search for the numeric position of the second space in A2, starting from the seventh character found in step 4. (11)
-
Subtract 1 from the character number of the second space found in step 4 to get the character number of “,”. The result is the ending position of the suffix. (11 — 1 = 10)
-
Search for the numeric position of the first space. (6)
-
After finding the first space, add 1 to find the next character (J), also found in steps 3 and 4. (7)
-
Take the character number of “,” found in step 6, and subtract the character number of “J”, found in steps 3 and 4. The result is the number of characters MID extracts from the text string starting at the seventh position, found in step 2. (10 — 7 = 3)
In this example, the first name is at the beginning of the string and the suffix is at the end, so you can use formulas similar to Example 2: Use the LEFT function to extract the first name, the MID function to extract the last name, and the RIGHT function to extract the suffix.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Gary Altman III |
First and last name with suffix |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (last name) |
‘=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-(SEARCH(» «,A2,1)+1)) |
=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-(SEARCH(» «,A2,1)+1)) |
Formula |
Result (suffix) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
-
First name
The first name starts at the first character from the left (G) and ends at the fifth character (the first space). Therefore, the formula extracts five characters from the left of the full name.
Search for the numeric position of the first space in A2, starting from the left. (5)
-
Last name
The last name starts at the sixth character from the left (A) and ends at the eleventh character (the second space). This formula involves nesting SEARCH to find the positions of the spaces.
The formula extracts six characters from the middle, starting from the sixth character.
Use the SEARCH function to find the value for start_num:
Search for the numeric position of the first space in A2, starting from the left. (5)
-
Add 1 to get the position of the character after the first space (A). The result is the starting position of the last name. (5 + 1 = 6)
Use nested SEARCH functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (5)
-
Add 1 to get the position of the character after the first space (A). The result is the character number at which you want to start searching for the second space. (5 + 1 = 6)
-
Search for the numeric position of the second space in A2, starting from the sixth character found in step 4. This character number is the ending position of the last name. (12)
-
Search for the numeric position of the first space. (5)
-
Add 1 to find the numeric position of the character after the first space (A), also found in steps 3 and 4. (6)
-
Take the character number of the second space, found in step 5, and then subtract the character number of “A”, found in steps 6 and 7. The result is the number of characters MID extracts from the text string, starting at the sixth position, found in step 2. (12 — 6 = 6)
-
Suffix
The suffix starts three characters from the right. This formula involves nesting SEARCH to find the positions of the spaces.
Use nested SEARCH and the LEN functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (5)
-
Add 1 to get the character after the first space (A). The result is the character number at which you want to start searching for the second space. (5 + 1 = 6)
-
Search for the second space in A2, starting from the sixth position (A), found in step 2. (12)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the second space, found in step 3. The result is the number of characters to be extracted from the right of the full name. (15 — 12 = 3)
In this example, the full name is preceded by a prefix, and you use formulas similar to Example 2: the MID function to extract the first name, the RIGHT function to extract the last name.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Mr. Ryan Ihrig |
With prefix |
Formula |
Result (first name) |
‘=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-(SEARCH(» «,A2,1)+1)) |
=MID(A2,SEARCH(» «,A2,1)+1,SEARCH(» «,A2,SEARCH(» «,A2,1)+1)-(SEARCH(» «,A2,1)+1)) |
Formula |
Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,SEARCH(» «,A2,1)+1)) |
-
First name
The first name starts at the fifth character from the left (R) and ends at the ninth character (the second space). The formula nests SEARCH to find the positions of the spaces. It extracts four characters, starting from the fifth position.
Use the SEARCH function to find the value for the start_num:
Search for the numeric position of the first space in A2, starting from the left. (4)
-
Add 1 to get the position of the character after the first space (R). The result is the starting position of the first name. (4 + 1 = 5)
Use nested SEARCH function to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (4)
-
Add 1 to get the position of the character after the first space (R). The result is the character number at which you want to start searching for the second space. (4 + 1 = 5)
-
Search for the numeric position of the second space in A2, starting from the fifth character, found in steps 3 and 4. This character number is the ending position of the first name. (9)
-
Search for the first space. (4)
-
Add 1 to find the numeric position of the character after the first space (R), also found in steps 3 and 4. (5)
-
Take the character number of the second space, found in step 5, and then subtract the character number of “R”, found in steps 6 and 7. The result is the number of characters MID extracts from the text string, starting at the fifth position found in step 2. (9 — 5 = 4)
-
Last name
The last name starts five characters from the right. This formula involves nesting SEARCH to find the positions of the spaces.
Use nested SEARCH and the LEN functions to find the value for num_chars:
Search for the numeric position of the first space in A2, starting from the left. (4)
-
Add 1 to get the position of the character after the first space (R). The result is the character number at which you want to start searching for the second space. (4 + 1 = 5)
-
Search for the second space in A2, starting from the fifth position (R), found in step 2. (9)
-
Count the total length of the text string in A2, and then subtract the number of characters from the left up to the second space, found in step 3. The result is the number of characters to be extracted from the right of the full name. (14 — 9 = 5)
This example uses a hyphenated last name. A space separates each name component.
Copy the cells in the table and paste into an Excel worksheet at cell A1. The formula you see on the left will be displayed for reference, while Excel will automatically convert the formula on the right into the appropriate result.
Hint Before you paste the data into the worksheet, set the column widths of columns A and B to 250.
Example name |
Description |
Julie Taft-Rider |
Hyphenated last name |
Formula |
Result (first name) |
‘=LEFT(A2, SEARCH(» «,A2,1)) |
=LEFT(A2, SEARCH(» «,A2,1)) |
Formula |
Result (last name) |
‘=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
=RIGHT(A2,LEN(A2)-SEARCH(» «,A2,1)) |
-
First name
The first name starts at the first character from the left and ends at the sixth position (the first space). The formula extracts six characters from the left.
Use the SEARCH function to find the value of num_chars:
Search for the numeric position of the first space in A2, starting from the left. (6)
-
Last name
The entire last name starts ten characters from the right (T) and ends at the first character from the right (r).
Use the LEN and SEARCH functions to find the value for num_chars:
Search for the numeric position of the space in A2, starting from the first character from the left. (6)
-
Count the total length of the text string to be extracted, and then subtract the number of characters from the left up to the first space, found in step 1. (16 — 6 = 10)
Need more help?
In an ideal world, you wouldn’t need to read this tutorial about some of the most important text formulas in Excel.
Unfortunately, we are not in ideal world…
If you work with Excel, you will need to know or learn how to use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions.
So… fortunately, you have found this tutorial which focuses on some of the most important text functions in Excel.
In this particular Excel tutorial, I explain step-by-step how you can use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel.
The following table of contents illustrates, more precisely, the topics we cover in this blog post. You can also use it to skip to the section that interests you the most.
This is a massive amount of content, so let’s begin by understanding…
Why You Should Learn How To Use The LEFT, RIGHT, MID, LEN, FIND And SEARCH Functions In Excel
You may rightly wonder why should you study this comprehensive 11,000+ word tutorial on Excel text formulas to learn how to use some of the most common Excel text functions.
There are several reasons why knowing how to use functions such as LEFT, RIGHT, MID, LEN, FIND and SEARCH is important. But instead of simply listing a bunch of reasons, allow me to ask you a question:
When working with Excel, there are times where you have to use data prepared by other people or companies. Based on your experience, how does this data look like? If you have never worked with data sets prepared by somebody else, please make a guess.
- Is it the data set clean, organized, and properly and consistently formatted?
- Or is it messy, disorganized, full of clutter and inconsistently formatted?
Let’s face it…
When you receive data prepared by other people or companies, you will need to clean up the data before you can proceed to work with it. In some cases, the data sets will be a complete mess.
And let’s not even talk about the situations where you have to work with several data sets, all prepared by different people who have completely different ideas about how the data should be organized (or disorganized).
When you use data from other sources, it’s usually not ready for analysis. Data scientists spend approximately 80% of their time in the process of gathering and cleaning up data before they can actually begin to analyze it.
The problem of messy data, however, is not exclusive to data analysts. There are many (other) examples. The main point to remember is that data quality is important. Therefore, knowing how to clean up data is essential.
What is the main take-away from the above?
You will need to clean up data. There is no question about it.
Sometimes you may also feel like this:
In order to avoid this type of situation, let’s ask a more relevant question: how will you clean up the data?
Will you do it manually? With the risks of missing errors or introducing new errors? Not to mention the amount of time cleaning up a large data set manually would take or how tedious the work would be…
Or will you automate the data clean-up process as much as possible to make it more streamlined and reliable?
If you are interested in learning how you can start automating the data clean-up process in Excel, keep reading. This Excel tutorial will make you significantly more efficient and productive, and reduce the risk of office violence.
I show you, step-by-step and using a very detailed example, how you can start using the LEFT, RIGHT, MID, LEN, FIND and SEARCH Excel functions now to improve your efficiency and productivity in Excel now.
You may be surprised by what you can do with Excel’s text functions. As explained by John Walkenbach in the Excel 2013 Bible:
(…) some of these formulas perform feats that you may not have thought possible.
Just as an example, quoting Excel Formulas and Functions for Dummies, mastering functions such as LEFT, RIGHT and MID “gives you the power to literally break text apart.”
Does this sound useful?
Then let’s start by understanding…
What Are Excel Text Functions
When you think about Excel, working with text is probably not the first thing that comes to your mind…
That is completely natural. After all, Excel is not a word processor… If you need a word processor, you can use Microsoft Word.
However, you may be surprised by Excel’s capabilities to handle and work with text. This is where Excel text functions come in…
Excel text functions are, as implied by their name, functions you can use to work with text or strings. For these purposes, as explained by John Walkenbach (one of the foremost authorities on Microsoft Excel) in the Excel 2013 Bible, text and string (and sometimes text string) are used interchangeably.
Despite the above, Walkenbach also explains how “many text functions are not limited to text” and, therefore, you can also use some of Excel’s text functions in cells that have numbers.
Excel has several text functions, which you can find by going to the Formulas tab of the Ribbon and clicking on “Text” or using the keyboard shortcut “Alt + M + T”.
What Is “Text”
I’ll ask you a question that may sound a little bit basic but, do you know what is text?
And I don’t mean regular text. I assume that, if you are reading this Excel tutorial, you know what is the usual definition of the word “text”.
The question is actually tricky because, in Excel, some things are slightly different from what you’d usually expect…
In Excel 2013 Formulas, John Walkenbach explains that whenever you type data, “Excel immediately goes to work and determines whether you are entering” one of the following:
- A formula.
- A number, which includes regular numbers, dates and times.
- Text, which is anything other than a formula or a number.
Believe it or not, it’s actually possible for Excel to treat a number as text. This is not that uncommon and, in some cases, can be quite annoying, such as when you are importing data into Excel.
It can also be quite dangerous as Excel does not always treat numbers that are formatted as text in the same way.
Let’s take a very simple example. The following Excel worksheet has two numbers in cells B1 and B2 (both numbers are 1). Cells B3 and B4 are reserved for the sum of these two numbers which you would expect to be 2 (as 1+1=2, right?).
Notice, however, that the numbers in cells B1 and B2 do not have the same alignment. The “1” in cell B1 is aligned to the left whereas the “1” in cell B2 is aligned to the right. This is because I have formatted cell B1 as text, something I may explain in future tutorials, and have not modified the alignment.
Now… let’s do some magic and see how, sometimes, 1 plus 1 doesn’t equal 2…
The screenshot below shows the result of calculating the sum of cells B1 and B2 using two different methods:
- In cell B3, the sum has been carried out by using the formula =B1+B2, as it appears in the parenthesis within cell A3.
- In cell B4, the sum has been calculated with the formula =SUM(B1:B2), as it appears inside the parenthesis in cell A4.
You can actually check out the formula bar to confirm that this is indeed the formula in the cell.
Don’t worry, you don’t have to go back to math lessons to review sums. Excel is indeed giving different treatments to cell B1 depending on which formula is used. In these cases, the SUM function treats cell B1 as a 0 and results in the sum of 1+1 not being equal to 2.
In this particular case, Excel indicates that there is a possible error (see the warning in the screenshot above) and the number in cell B1 is stored as text. According to John Walkenbach in Excel 2013 Formulas, this is usually the case (that Excel identifies the cell) if you have background error checking enabled.
However, as Walkenbach himself explains:
(…) be aware of Excel’s inconsistency in how it treats a number formatted as text.
Perhaps even better, be careful in general when using the text format and, when in doubt, use the general format.
Now that you know what exactly is “text” within Excel, allow me to introduce the example that I use in this tutorial on Excel text formulas:
My main business (as of the time of writing of this blog post) is real estate.
As you can imagine, our real estate company has to work a lot with addresses. Addresses for plots of land, buildings, houses and so on. You get the idea…
Sometimes we receive these addresses in big data sets prepared by somebody else and, as you may expect, their idea about how a database should be organized is different from mine.
Therefore, we constantly have to go through the process of cleaning up and fixing address data.
Address data is not only relevant for the real estate and construction industry. Databases where addresses can appear include, for example, the following:
- If you focus on sales, you may work constantly with lists of customers and may have to keep an organized list with their contact details.
- If you work at a company that operates several shops or branches, you may need to keep track of their individual addresses.
Therefore, the example I use to explain you how to use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel focuses on address data. More precisely, I take an address that I receive in a particular format and split the address into its different parts using Excel.
For these purposes, I have created 1,000 random addresses using a random address generator. They have all been pasted into a single column of an Excel worksheet.
This Excel Text Functions Tutorial is accompanied by Excel workbooks containing the data and formulas I use. You can get immediate free access to these example workbooks by subscribing to the Power Spreadsheets Newsletter.
The column with the full addresses in the initial Excel worksheet looks as follows:
Do you see any problems with the way this address list is organized?
Granted… it’s possible to get messier data. In this case all the addresses follow the same format, names are properly capitalized and, generally, the data looks relatively clean.
However, the data is organized in a way that may make it difficult to analyze using more advanced tools such as filters or PivotTables (topics I may explain in future tutorials). For example:
- Every single address is divided in two rows. The top row shows the street and number while the second row includes the city, state and zip code.
This is not very convenient if, for example, you want to have all the data of each customer in a single row. - Within a row, there are several details. As mentioned above, the first row includes both the street and number, and the second row lists the city, state and zip code.
Depending on the type of analysis you want to carry out with the data, it may be more useful if you are able to split these elements and show each item in a separate cell.
As a consequence of the above, I show you how to:
- Convert all addresses into a single row.
- Separate the elements of each address so that each item has its own individual cell.
More precisely, I show you step-by-step how to fill out the following table using Excel text formulas:
The steps in this example are chosen and structured considering that the main purpose is to show you, going slowly and step-by-step, how to use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel so you can apply and adapt these formulas by yourself later in different situations. There are other ways to achieve the same or similar results by using methods such as nesting functions within other functions, the text to columns command or, in some cases, slightly simpler formulas or tools that do not involve the LEFT, RIGHT, MID, LEN, FIND or SEARCH functions.
In certain cases, those other methods may be more appropriate and efficient to achieve your particular objectives. For example, instead of creating several columns (as I do in the process below), you can create a few nested functions that carry out all the required processes in less cells or use the text to columns command to parse the data.
However, nested functions, text to columns and similar tools deviate from the main purpose of this Excel tutorial: showing you how to use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions.
I agree, however, that there are different ways to perform the actions described in this Excel tutorial and I’m very interested in hear which other methods you would use to organize the addresses above. Please share your ideas and comments about how you would improve the process in the comments at the end of this guide.
I also mention and use some functions than are not explained in depth in this Excel tutorial, such as VALUE, ISNUMBER, IF and CONCATENATE. I may cover them in detail in future tutorials.
Are you ready?
Then let’s go on to the step-by-step explanation of how to use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel to fix these addresses.
Step #1: How To Use The LEFT Function In Excel To Determine Whether The First Character In A Cell Is A Number
One of the main problems with the original address list is that each address is spread over two rows of data.
Therefore, in the first few steps of this example, I show you how to solve this problem and get each address into a single row.
If you take a close look at the addresses, you’ll notice that the first row (where the street and number are) always begins with a numeric character while the second row (where the city, state and zip code appear) begins with an alphabetic character.
You can use this fact to distinguish between the rows of a single address. More precisely, you know that:
- If the first character in a cell is a number, this is the first row of the address.
- If the first character in a cell is not a number, this is the second row of the address.
So, the first thing you want Excel to do is determine whether the first character in a cell is a number or not.
I explain below, step-by-step, how you can do this:
1. Step #1: Use The LEFT Function To Grab The First Character Of The String.
The LEFT function allows you to find what the first characters in a text string are. You get to choose the number of characters that the LEFT function returns.
What is the syntax of the LEFT function?
The syntax of the LEFT function is “LEFT(text,num_chars)”, where:
- “text” is the text from which you want to extract the characters.
Here is where you tell Excel the location of the string from which you want to grab the characters or type the text within quotes “” (for example “This is the best Excel tutorial”).
- “num_chars” is the number of characters you want to extract.
The main requirement that num_chars must meet is that it can’t be a negative number. You can, however, specify a number of characters that is larger than those contained in the original string. In this case, the LEFT function simply returns the whole text.
You can also leave num_chars blank (omit it), in which case the LEFT function assumes it is 1 and return the first character of the text string.
So, how do you use the LEFT function to grab the first character of the addresses that are being cleaned up in the example?
For illustration purposes, I first create an additional column in the Excel worksheet that contains the addresses. This is column C and is titled “First Character”.
Now, let’s take a look at the syntax of the LEFT function, “LEFT(text,num_chars)”. In this case:
- “text” is the text located in column B.
- “num_chars” is 1.
It’s also possible to leave num_chars blank so that Excel assumes it is 1.
So the LEFT function is “LEFT(B#,1)”, where # is the number of the relevant row. For example, the formula for cell C3 is “=LEFT(B3,1)”:
And, in this case, the LEFT function returns 8, which is the first character of the address 857 King Street.
Now, you can copy and paste this formula across the 2,000 rows of data to get the LEFT function to return the main character of every single cell in the address list.
2. Step #2: Convert The Character You Have Extracted From The Text String Into A Number.
You are already aware of how, sometimes, Excel does not treat numbers as numbers. Remember how, above, I showed you that sometimes 1+1 is not equal to 2.
A similar thing may happen to the cells where the output of the LEFT function are.
For example, if you were to use the ISNUMBER function, which allows you to check if a value is a number and returns TRUE (if it is a number) or FALSE (otherwise) on cell C3 (which displays the number 8), the results are as follows:
Meaning that, according to Excel, the value in cell C3 is not a number.
To ensure that this does not cause any problems down the road, I convert the character that has been extracted from the text string to a number using the VALUE function. The VALUE function converts any text string that represents a number to an actual number for Excel purposes.
I may cover the VALUE function in detail in future Excel tutorials.
For the moment, is enough to know that the VALUE function has a single parameter which is the text that is converted to a number. In the example used in this Excel tutorial, this is the text located in column C.
Before applying the VALUE formula I add a new column to the Excel worksheet. This is column D titled “First Character Value”.
In this case, the formula for cell D3 is “=VALUE(C3)”.
And the result is the number 8:
You can copy and paste the formula in all the cells of column D to convert all of the text values that represent numbers in column C to actual numbers. Note that, in the cases where column C does not contain a number, but has a letter, the VALUE function returns “#VALUE”.
I fix this below.
3. Step #3: Check Whether The Character You Have Extracted Is A Number Or Not.
The ISNUMBER function allows you to check if a value is a number. If the value is indeed a number, ISNUMBER returns TRUE. If the value is not a number, ISNUMBER returns FALSE.
You can use the ISNUMBER function to check whether the first character you have extracted from the column of addresses is a number or not.
I may explain more about the ISNUMBER function in future Excel tutorials but, for the moment, is enough to know that ISNUMBER has a single parameter. This parameter is the particular value that you want Excel to test.
As usual, let’s add a new column to the Excel worksheet. This is column E and its title is “Is First Character a No.?”.
The formula for cell E3 is “=ISNUMBER(D3)”.
And the function returns TRUE, indicating that the value in cell D3 (and therefore the first character in the address) is a number.
You can copy and paste the formula in all the cells of column E to have Excel evaluate the values of the first character of the original text string. Notice how Excel, correctly, returns TRUE in the cases where the first character of the address cell is a number and FALSE when the first character is not a number.
If column E displays TRUE, that row is the first row of a particular address. This is because the first row of each address (where the street and number are) always begins with a number.
On the other hand, if column E shows FALSE, that particular row is the second row of an address (where the city, state and zip code appear) since those rows begin with alphabetic characters.
As a consequence of the above, you’re ready to move to the second step of cleaning up the address data…
Step #2: How To Use The IF Function In Excel To Place Each Full Address In A Single Row Of The Excel Worksheet
This particular step doesn’t focus on the functions and tools that are the main subject of this Excel tutorial. However, this step is important as it completes the process of putting each full address (which is originally divided in two rows) in a single row.
So let’s go ahead and do this…
1. Step #1: Use The IF Function To Get The First Part Of Each Address.
More precisely, you’re seeking to get the the street and number for each address.
The IF function allows you to check whether a condition is true or not and, based on the result, does one thing or another. Therefore, you can choose what the IF function should return if the condition is true or false.
For purposes of this tutorial, is important to understand its basic syntax: “IF(logical_test,value_if_true,value_if_false)”, where:
- “logical_test” is the condition you want to test.
- “value_if_true” is the value that the IF function returns if the condition you are testing is true.
- “value_if_false” is the value that the IF function returns if the condition you are testing is false.
How can you use this to get the first part of each address?
The first thing you probably want to look at is column E, since this tells you which rows have the first part of each address. Those rows are the ones where column E shows TRUE since, in those cases, the first character is a number (and all addresses begin with a number).
Therefore, you can use the IF function to:
- Test whether the value that appears in column E is equal to TRUE. This is the logical_test.
- If the condition you are testing is true, which is the case if that particular row contains the first part of an address, print that first part of the address (the street and number which appear in column B).
- If the condition you are testing is false, which happens if that row has the second part of an address, print nothing or leave the cell blank. This can be achieved by using quotes (“”).
How does this look in practice?
Let’s go back to the Excel worksheet…
First, I add a new column. This is column F and is titled “First Part of Address”.
The IF formula for cell F3 is “=IF(E3=TRUE,B3,””)”.
And, as expected, this returns the first part of the desired address.
You can copy and paste this formula in all the relevant cells. Check out how, as planned, column F displays the first part of the address or is blank.
2. Step #2: Use The IF Function To Get The Second Part Of The Address.
This time, you’ll be using the IF function to get the city, state and zip code for each address.
This step is basically the same as above with a couple of small tweaks. In this case, you can use the IF function as follows:
- Test whether column E shows TRUE. This is the same logical test used in the previous step.
- If the condition is true, print the second part of the address (the city, state and zip code that is displayed in column B).
Here is the main change in the syntax of the IF function when compared to the syntax used in the previous step. In the previous step you referred to the same row of the active cell whereas, now, you refer to one row below the active cell.
- If the condition you are testing is false, print nothing or leave the cell blank. This is exactly the same result as in the previous step.
Let’s start by adding an additional column to the Excel worksheet. This is column G and its title is “Second Part of Address”.
The IF formula for cell G3 is “=IF(E3=TRUE,B4,””)”. Notice that this is exactly the same formula used in the previous step except for the second parameter which has changed from cell B3 (same row as active cell) to cell B4 (one row below the active cell).
And, as planned, Excel returns the second part of the relevant address.
Just as in the previous step, you can copy and paste this formula to the 2,000 rows of column G. The results are substantially similar: Excel either returns the second part of the address or leaves the cell blank.
Step #3: How To Use Filters And Sorting To Delete Blank Rows In Excel Without Loosing Data
You may have noticed that, now that all addresses are in a single row, half of the cells in columns F and G are blank.
Those rows are no longer useful for purposes of fixing the addresses and, therefore, I show you how to delete them without loosing any data by using filters and sorting.
1. Step #1: Copy And Paste The Values Of All The Cells Located In Column G.
If you take a closer look at the IF formulas in column G, you’ll notice that they always make reference to a cell located in the row immediately below. As a consequence of this, if you delete all the rows that have blanks, you also delete the cells to which these formulas make reference to. For example, cell G3 makes reference to cell B4, as shown in the image below:
Deleting these references without any previous preparations leads to invalid cell reference errors appearing in column G. For example, in the case above:
You can avoid this type of error by copying all of column G and pasting its values.
To do this, proceed as follows. Below the step-by-step explanation there is an image illustrating how to perform all of these steps.
- Step #1: Click on the column letter header G.
- Step #2: In the Home tab of the Ribbon, click on “Copy”.
- Step #3: In the same Home tab, click on the drop-down menu button below “Paste”.
- Step #4: Once the drop-down menu expands, click on “Values”.
- Step #5: Excel pastes hard-coded values (not formulas) on all cells of column G.
Once you have done this, the cells in column G won’t have any formulas, just values. For example, in the case of cell G3:
2. Step #2: Using Filters, Sort Column F Or G From Highest To Lowest.
I may explain filters in more depth in future tutorials. For the moment is enough to know that you can use the sorting and filtering tools of Excel for purposes of rearranging your data, as shown below.
To do this, proceed as follows. I include an image below the step-by-step explanation showing how you can do this in Excel.
- Step #1: Select the headers of the table which, in this case, are located in row 2.
- Step #2: Go to the Data tab of the Ribbon and click on “Filter”.
- Step #3: Excel shows a drop-down arrow next to each header, showing that the filters are enabled but have not been applied.
- Step #4: Click on the drop-down arrow to the right of “First Part of Address” in cell F2 or “Second Part of Address” in cell G2.
- Step #5: When the full drop-down menu appears, click on “Sort Z to A”.
- Step #6: Excel sorts all the columns of the table based on the values of the column whose drop-down menu you have used for sorting purposes.
Why have I asked to sort the data like this?
If you scroll down to the middle of the table, you notice that the rows whose columns F and G appear empty are all in the lower half of the table.
This means that the 1,000 addresses are now all in the first 1,000 rows of the table and, therefore, you can proceed to eliminate rows 1,001 to 2,000.
3. Step #3: Delete Rows That Have Blank Cells In Columns F And G.
You can proceed as follows to delete all the rows that have blank cells in columns F and G. The image below the explanation illustrates all of these steps.
- Step #1: Select all of the rows to be deleted. You can do this by, for example, clicking on the row number header of the first row to be deleted, and then using the keyboard shortcut “Shift + Ctrl + down arrow”. This may take you all the way to the end of the Excel worksheet; don’t worry about it.
- Step #2: Right click on the row number headers.
- Step #3: A context menu appears.
- Step #4: Click on “Delete”.
Excel deletes all chosen rows. Now the table only has 1,000 rows, each corresponding to one of the 1,000 addresses.
4. Step #4: Disable The Filters.
You can disable the filters by clicking on “Filter” in the Data tab.
This step is optional; you can also carry on with the rest of this Excel tutorial while having the filters enabled.
Step #4:How To Use The RIGHT Function In Excel To Determine Whether The Last Character In A Cell Is A Space
Before I begin this section, I have to make the following clarification:
Usually you won’t use the method described in the following 3 steps (including steps #5 and #6) to deal with leading and trailing spaces in a text string. Instead of this, you’ll generally use the TRIM function.
However, I believe carrying out all activities with the functions we’re focusing on (LEFT, RIGHT, MID, LEN, FIND and SEARCH) gives you a better idea of the different things you can achieve with them.
Before we carry on, allow me to ask you the following question:
Based on the screenshot above, would you be able to say with certainty that there are no extra blank spaces at the end of an address?
For example, let’s focus on the first address: 999 River Street.
Would you be able to say with certainty that the letter t in Street is the last character and there are no blank spaces after it?
It is a tough question to answer, isn’t it?
And it is not a trivial question. As explained by John Walkenbach in Excel 2013 Formulas, extraneous spaces can cause problems in some cases, such as when using lookup formulas.
Fortunately, you can use the RIGHT function to take the guess work out of this.
The RIGHT function is, as you may expect, substantially similar to the LEFT function that I explained above. More precisely, the RIGHT function allows you to get the last (further to the right) characters in a text string. You decide, and tell Excel, how many characters it should return.
The syntax of the RIGHT function is, basically, the same as that of the LEFT function: “RIGHT(text,num_chars)”, where:
- “text” is the text string from which you want to get the characters.
This can be either the location of the text from which you want to extract the characters or a string within quotes (such as “I love Microsoft Excel”).
- “num_chars” is the number of characters you want the RIGHT function to give you back.
How many characters can you specify?As long as you don’t specify a negative number (which is not allowed), you should be fine.
Just note that, if you specify a number of characters that is larger than the length of the text string, the RIGHT function simply returns the whole string. Additionally, if you fail to specify a number, Excel assumes its 1 and return the last character of the string.
So let’s go ahead and use the RIGHT function to determine whether any of the two parts of the addresses has a blank space at the end.
As in previous occasions, I start by adding two columns to the right of the Excel worksheet. These are columns H and I, and are named “Last Character of First Part” and “Last Character of Second Part” respectively.
The formula for cell H3 is “=RIGHT(F3,1)”.
And the formula for cell I3 is “=RIGHT(G3,1)”.
You’ll notice that, according to the RIGHT function, the last character of the second part of the address is the number 2. This result was expected, as that is the last character of the zip code.
However, the cell with the last character of the first part shows nothing, meaning that it’s not the letter t but rather a space.
You can copy and paste these two formulas across all the rows of the table to see whether the other addresses have the same characteristic (the last character of their first part is a space).
As you can see in the results below, all of the cells in column H have no visible characters. They’re all spaces.
Sigh…
This means that one of the next steps in the process is to eliminate the extra space at the end of the first part of each address.
Could you do it using some of the functions that you have already learned in this tutorial?
Yes!
You can, for example, use the LEFT function to grab all the characters of the first part of the address except for the last one. In the next two steps, I show you one of the ways this can be done.
There are other ways to achieve this goal such as, for example, using the TRIM function. However, this is a topic that I’ll cover some other time.
Step #5: How To Use The LEN Function In Excel To Calculate The Number Of Characters In A Text String
Do you recall the syntax of the LEFT function?
That’s right, it’s “LEFT(text,num_chars)”.
If your purpose is to extract all the characters from the first part of the address except for the last one, the first question you may have is: how can you figure what is the character length of each of those strings of text?
That is a reasonable question. After all, the length of each address varies.
In this section, I introduce you a function that allows you to count the number of characters in a text string: the LEN function.
The LEN function counts and informs you what is the number of characters in a string. In other words, it tells you how long the text string is. The syntax of LEN is “LEN(text)”, where “text” is simply the string whose length you want to know.
Note that spaces, such as the ones that appear at the end of the first part of the addresses in the sample data set or between words, are also counted as characters.
As explained by Ken Bluttman in Excel Formulas and Functions for Dummies, LEN is usually used in conjunction with other functions such as LEFT, RIGHT or MID. In those cases, LEN is regularly used to set one of the parameters of the other function. I may explain in future tutorials how you can do this to create even more powerful Excel text formulas.
Before using the LEN function to calculate the number of characters in the first part of each address, I insert a new column in the Excel worksheet. This is column J and is named “Length of First Part of Address”.
Now you can proceed to measure the length of the first part of each address by using the LEN function. For example, the formula for cell J3 is “=LEN(F3)”
And LEN returns the number of characters in the first part of the address.
You can then, as usual, copy and paste the formula in all the relevant cells of column J to get Excel to calculate the length of the first part of each address.
Now that you have the length of the first part of each address, you can simply subtract one from each number to get the number of characters that you should extract from each string of text (using the LEFT function) to eliminate the extra space at the end.
For these purposes, I simply add a new column to the Excel worksheet (column J, titled “Proper Length of First Part of Address”) where I subtract 1 from the values obtained above.
After copying and pasting this formula in all the appropriate cells, the table looks roughly as follows:
Step #6: How To Use The LEFT Function To Get All The Characters In A Text String Except The Last One
By now, you have learned how to use the LEFT function in Excel and you know how many characters you need to extract from the first part of each address in order to exclude the last character.
So… do you know how to use the LEFT function to get all the characters, excluding the last one, in the first part of an address?
Let’s go through this process together…
First, I add a new column to the address table. This is column L and its title is “Proper First Part of Address”.
You already know the syntax of the LEFT function. For example, the formula for cell L3 is “=LEFT(F3,K3)”.
And Excel returns the first part of the address, excluding the last character. In this case, this means that the text string doesn’t have the extra space at the end.
You can then copy and paste it for all the rows in the table.
For organizational purposes, I add a new column to the table (column M with the title “Second Part of Address”), where I put the second part of each address. This way, the first and second part of the address are next to each other.
Step #7: How To Use The FIND And SEARCH Functions In Excel
Things are looking good, aren’t they?
Each address is in a single row and there are no extra spaces or extraneous characters.
However, the final objective of the example has not yet been achieved. I said at the beginning of this Excel tutorial that I’d show you how to fill out the following table using Excel text formulas:
In order to do this, I need to be able to extract each of these elements from the two parts of the addresses that currently appear in columns L and M.
As you probably imagine, it is possible to:
- Use functions such as LEFT and RIGHT to get the desired parts of each text string.
- Use LEN to calculate the number of characters to be extracted by the LEFT and RIGHT functions.
However, in this particular case, the situation is slightly more complicated than when using the LEFT and LEN functions to get the first part of the address without the last character.
The reason for this is that:
- In the previous steps, the length of the whole text string varied, but the number of characters to be excluded was fixed (it was always 1).
- Now, both the length of the text string and the length of the components to be included or excluded varies.
For example, street addresses (both the street and the number) and city names have different lengths.
State names have all the same length (2 characters) but, in order to be able to extract them using Excel text formulas (more precisely, using the MID function which I explain below), it’s necessary to know the position of the first character which, in turn, depends on the city name length.
Zip codes are more manageable. They are all 5 characters long and sit at the end of the string. Therefore, as you may have guessed, it is possible to extract them with the RIGHT function.
In any case, it’s necessary to find a way to measure the length of each of the different elements of the addresses.
How could you do this?
Allow me to introduce the FIND and SEARCH functions…
The FIND And SEARCH Functions In Excel
The FIND and SEARCH functions in Excel are substantially similar. Their purpose, as you may imagine from their names, is to find a particular piece of text within a text string.
What does this mean?
Let’s look at the rough idea graphically. Assume you are reading a book and you see the following text:
Now, imagine that you want to find the string of text “state Department”.
If you had the possibility to use the FIND or SEARCH functions of Excel, you could tell them something like: find the text string “state Department” within this page.
And Excel would tell you the location of the string “state Department”.
Now that you know the basic idea behind the FIND and SEARCH functions in Excel, let’s take a closer look at how they work in practice. From the explanation above, it is already clear that the main purpose of both FIND and SEARCH is to locate a particular text string within a longer text string.
How does Excel tell you the location of the text string you want to locate?
The FIND and SEARCH functions tell you where is the string you are searching for by returning a number that represents the starting position of that text string. Let’s take, for example, the first address in the sample data set: 999 River Street and assume you want to know the location of the word “River”.
In this case, Excel returns the number 5 because the starting position of the word “River” is the fifth character. The first 4 characters are 9, 9, 9 and space.
Let’s take a look now at the syntax of the FIND and SEARCH functions in Excel. The syntax of FIND is “FIND(find_text,within_text,start_num)” whereas the syntax of SEARCH is “SEARCH(find_text,within_text,start_num)”. In both cases:
- “find_text” is the string that you want to find. In the cases above, it is “state Department” or “River”.
- “within_text” is the text string where you want to search. In the cases above, it is the whole page or the Proper First Part of Address in the sample Excel worksheet.
- “start_num” is the character at which you want to start to search for the find_text within the within_text.
This argument is optional. If you leave it blank, Excel assumes it is 1 and begins the search on the first character of the within_text.
start_num is useful if, for example, you are only interested in the find_text that appears towards the end of a particular string of text.
For example, in the case of 999 River Street above, you may want to find the location of the second space (the one that separates the words “River” and “Street” but are not interested in knowing where the first space (that separates 999 from River) is. In such a case, you can set start_num to a number such as 6 (which, as explained above, is the starting position of the word “River”) and the FIND or SEARCH function return the number 10.
In fact, if you continue reading, you’ll notice that this is precisely one of the methods that I use below.
You probably have an additional question regarding the FIND and SEARCH functions…
Are FIND and SEARCH then the same function? Is there any difference between them?
Good question. There are, indeed, a couple of differences between FIND and SEARCH. The main ones are the following:
- FIND is case-sensitive whereas SEARCH is not.
For example, using the text from the cases above, FIND distinguishes between “state Department” and “state department” or between “River” and “river”. The SEARCH function doesn’t make such a distinction.
- SEARCH allows you to use the wildcard characters “?” (question mark) and “*” (asterisk) in find_text (the text you are searching for), whereas FIND does not.
What is the purpose of these wildcard characters?The question mark (?) character matches any single character. For example, in the case of 999 River Street above; if instead of searching for “River” you search for “R?ver”, Excel also searches for variants such as “Rover”.
The asterisk (*) character matches any sequence of characters. Continuing with the case of 999 River Street; if instead of searching for “River” you type in “Riv*”, Excel also looks out for different words that begin with “Riv” such as “Riviera”.
What should you do if you want to actually search for a question mark (?) or asterisk (*)?Type a tilde “~” before the question mark or asterisk. For example, if you want to search for “River?”, type “River~?” and if you want to search for “River*”, type “River~*”.
And… what should you do if you want to search for a tilde (~)?
Following with the logic of adding tildes before the special characters, in this case you need to type 2 tildes (~~).
Now you that you have a good understanding of both the FIND and SEARCH functions, let’s take a look at…
How To Apply The FIND Or SEARCH Functions To The Sample Data Set
The main purpose of introducing the FIND and SEARCH functions to you is to allow you to measure the length of each of the different elements of the addresses so that, in the following steps, you can get each individual item using the LEFT, RIGHT and MID functions.
Below, I explain to you how to do this.
Please note that I use and make reference to the SEARCH function. Theoretically, for this particular case, you can also use the FIND function since the searches do not use wildcard characters and there are no lowercase nor uppercase letters.
1. Step #1: Use The SEARCH Function To Locate Spaces And Commas.
Use SEARCH to find the location of:
- The first space in the first part of the address. This is the space that separates the street name from the number.
For example, in the case of the first address:
- The comma (,) in the second part of the address. This is the space that separates the city from the state.
For example, using the same case as above:
- The last space in the second part of the address. This is the space that separates the state from the zip code.
Continuing with the same example:
For clarity purposes, I add three new columns to the table. These columns are N, O and P. Their titles are “Location of First Space in First Part”, “Location of “,” in Second Part” and “Location of Last Space in Second Part”.
The formulas for the first two columns are relatively straightforward. After all, you know what is the text you are searching for (a space or a comma), what is the text you are searching in, and that you are searching from the beginning of the text.
Therefore, the formula for cell N3 is “=SEARCH(” “,L3)”.
Similarly, the formula in cell O3 is “=SEARCH(“,”,M3)”.
Both formulas return the expected results. For example, the location of the first space in the first part of the address is 4 (the first 3 characters are 9, 9 and 9) and the location of the comma in the second part is 7 (the first 6 characters are E, a, s, t, o and n).
The syntax of the third SEARCH formula, whose purpose is to find the last space in the second part of the address, is slightly more complicated.
If you take a close look at the second part of each address, you’ll notice that there are at least two spaces: one after the comma (,) that separates the city from the state, and one that separates the state from the zip code. There may be additional spaces if the city name is two words or longer.
You want the SEARCH function to return the location of the last space. This means that you need to specify a third parameter in the SEARCH function: the optional start_num argument which specifies the character at which you want to begin the search.
This is, however, not that simple. After all, city names have different lengths so you can’t just fix start_num.
To solve this small issue, let’s add a couple of intermediate steps.
First, I add an extra column in the Excel worksheet between the current columns O (Location of “,” in Second Part) and P (Location of Last Space in Second Part). This new column has the name “Beginning of State” and, as you see later, it comes in handy for other purposes in addition to finding the location of the last space in the second part of the address.
How do you know the number of the character at which the state begins?
You already know what is the number of the character at which the comma (,) that separates the city from the state is. This is the value that appears in column O (Location of “,” in Second Part).
Additionally, if you look closely at the format of the second part of all addresses, you’ll notice that after the comma there is a space (which is the first space in the second part). The state begins after that space.
As you have probably realized by now, the state begins 2 positions after the comma. Therefore, to find the number of the character at which the state begins you just need to add 2 to the value that appears in column O.
For example, the formula for cell P3 is “=O3+2”.
Once you know the position at which the state begins, you know that the next space is the last space in the second part of the address.
Therefore, you can use the position of the first character of the state as the starting point for the search of the last space in the text string (the start_num argument).
Considering the above, you can build a slightly more complex SEARCH function to find this last space. For example, the formula for cell Q3 is “=SEARCH(” “,M3,P3)”.
The newly added columns return the values that were expected so you can copy and paste the formulas in all the cells of columns N, O, P and Q to get all the relevant locations for each address.
2. Step #2: Determine Length Of Components Of First Part Of Address.
Use the data you have already calculated to determine the length of the different components in the first part of the address: namely the street name and number.
If you go back to column K of the Excel worksheet, you find the total length of the first part of the address after excluding the extra space at the end.
Additionally, in the step immediately prior to this one, you found the location of the space that separates the number and the street name. This value appears in column N of the Excel worksheet.
Using these two values, you can determine:
- The length of the number in the first part of the address.
You can find this by subtracting 1 from the location of the first space. This makes sense, since that space is just (1 character) after the end of the number
- The length of the street name.
You can calculate this by subtracting the location of the first space from the length of the whole text string. This makes sense, since that space is immediately (1 character) before the beginning of the street name.
Let’s do this…
For clarity purposes, I add (again) two new columns to the table in the Excel worksheet. These are columns R and S. Their titles are “Length of No.” and “Length of Street Name”.
And then I type in the relevant formulas. For example, the formula for cell R3 is “=N3-1”.
And the formula for cell S3 is “=K3-N3”.
As usual, you can simply copy and paste these formulas in the rest of the cells of those two columns to get the lengths of the street names and numbers for the rest of the addresses.
3. Step #3: Determine The Length Of The City Name.
You may wonder…
Wait! What happens with the length of the state and zip code? Don’t we need to calculate those too?
You can calculate the length of the states and zip codes for practicing purposes. However, this is not strictly necessary because they are always the same. More precisely:
- States are always 2 characters long.
- Zip codes are always 5 characters long.
So in reality, it’s only necessary to calculate the length of the city name.
Do you know how to do this calculation?
Hint: it’s very similar to the way we calculated the length of the number in the first part of the address.
Before you answer, let me add a new column to the Excel worksheet. This is column T and I name it “Length of City”.
You may have answered that the calculation of the length of any city name requires that you subtract 1 from the location of the comma in the second part of the address.
If this was your answer, you are correct! This makes sense due to the fact that the comma is immediately (1 character) after the end of the city.
Therefore, for example, the formula for cell T3 is “=O3-1”.
You can then proceed to copy and paste this formula in all the other rows of the table to have Excel calculate the length of each city name.
Step #8: How To Use The LEFT, RIGHT And MID Functions In Excel To Get The Individual Components Of Each Address
I am aware that this has been a long tutorial…
But you’re close to finishing.
This is the step, where you actually extract each of the components of each address to the table where each of them has its own column.
You can do most of this by using the LEFT and RIGHT functions which have been explained in this Excel tutorial. You, however, need to use the MID function to get the State data.
So, before going into the actual process of getting the data, let’s take a look at the MID function…
The MID Function In Excel
The MID function works similarly to the LEFT and RIGHT functions.
In particular, its purpose is substantially the same: to grab a certain amount of characters from a text string.
Why do you need the MID function if you already know how to use the LEFT and RIGHT functions?
You already know that you can use the LEFT function to get the first (furthest to the left) characters of a piece of text and the RIGHT function to extract the last (furthest to the right) characters of a string.
But… how can you get characters from the middle of a text string?
For example, in the case of the address database that has been used as an example in this Excel tutorial, how can you get the state data?
Theoretically, you can first apply the LEFT function to get the left portion of the address (up to and including the state) and, then, the RIGHT function to get the state. You can also do it the other way around: apply the RIGHT function followed by the LEFT function…
But using the MID function is simpler and doesn’t require you to go through multiple steps.
More precisely, the MID function returns a certain number of characters from the middle of a piece of text. It does this by starting at whichever position you specify in the formula and grabbing the amount of characters you determine.
The syntax of the MID function is “MID(text,start_num,num_chars)”, where:
- “text” is the string from which you want to get the characters.
- “start_num” is the position of the first character you want the MID function to get.
start_num can’t be a negative number.
Ideally, it should also be less than the length of the text from which the MID function is extracting the characters. The reason for this is that, if start_num is larger than the length of the text, the result returned by the formula is “” (empty).
Finally, as suggested by Ken Bluttman in Excel Formulas and Functions for Dummies, start_num should be a number larger than one. Why is this? Because, if you needed to begin to extract text from the beginning of the string, you could simply use the LEFT function.
- “num_chars” is the number of characters, or length, of the text string you want the MID function to return. This value can’t be a negative number.
If, for some reason, start_num (position of first character to be extracted) plus num_chars (number of characters to be grabbed) is more than the length of the text string from which the function is getting the characters, the MID function returns all the characters up to the end of the text. In this case, it has a similar effect to applying RIGHT (although the syntax is certainly different).
How To Use The LEFT, RIGHT And MID Functions To Get The Individual Components Of Each Address
By now you know how to use the LEFT, RIGHT and MID functions in Excel. You have also applied the LEFT and RIGHT functions to a couple of different situations.
So let’s go ahead and apply them to get the individual components of each address and fill the following table:
For ease of reference, I add the columns above to the left of the table in the original Excel worksheet. They are columns U through Z.
1. Step #1: Use The LEFT Function To Fill The “No.” Fields.
You know the text from which you want to extract the data (column L; Proper First Part of Address) and how many characters to get (column R; Length of No.).
You can build the relevant LEFT function with these two arguments. For example, the formula for cell V3 is “=LEFT(L3,R3)”.
Notice that the value returned by the LEFT function for the first address (999 River Street) is as expected (999).
2. Step #2: Use The RIGHT Function To Fill The Street Fields.
The text from which you want to get the data is the same as above (column L). In this case, the amount of characters to get appears in column S (Length of Street Name).
These two parameters are all you need to build the relevant RIGHT formula. For example, in the case of cell U3, the relevant formula is “=RIGHT(L3,S3)”.
Just as in the previous case, the text returned by the RIGHT function for the first address is the correct one (River Street).
3. Step #3: Fill The City By Using The LEFT Function.
The city is the first item in the second part of the address. Therefore, in this case, the string from which you want to extract the data is column M (Second Part of Address). The amount of characters to get is in column T (Length of City).
You are now ready to insert the LEFT formula. As an example, for cell W3, the formula is “=LEFT(M3,T3)”.
Once again, the formula returns the appropriate data (Easton).
4. Step #4: Use The MID Function To Get The Data For The State Fields.
Finally!
The chance for you to test the MID function.
Let’s recall the syntax of the MID function: “MID(text,start_num,num_chars)” and analyze each item separately:
- The string from which you want to extract the characters is the same as that from which you have gotten the city. Therefore, in the first argument, you make reference to column M.
- Maybe you remember that, when explaining how to use the FIND and SEARCH functions in Excel, I added column P (Beginning of State) and calculated what was the location of the first character of the state.
I also mentioned that this value would be useful later… and here it is the moment where you get to use it again.
You know that the position of the first character you want the MID function to get is in this column P. Therefore, you have the second argument for the formula.
- Finally, you know that the states are always 2 characters long.
Therefore, you are ready to insert the appropriate MID formula. For example, in cell X3, this is “=MID(M3,P3,2)”.
And Excel returns the correct values.
5. Step #5: Use The RIGHT Function To Obtain The Zip Code Data.
The zip code is the last item to extract.
As anticipated above, you are using the RIGHT function to extract the zip code.
The text from which you extract the data is the same as in the two cases above (column M). All zip codes are 5 characters long. This is all the data you need to create the relevant RIGHT function.
For example, for cell Y3, the formula is “=RIGHT(M3,5)”.
And you get the correct zip code as a result.
6. Step #6: Copy And Paste Formulas.
Copy and paste all the formulas in cells U through Y all the way down to the end of the table.
Congratulations!
You have completed all the steps of this Excel Text Formulas Tutorial that are related to how to use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel.
Additionally, your table is almost complete…
If you have the energy for a final step, I show you how to fill the last column of the table above…
Step #9: How To Use The CONCATENATE Function In Excel To Join All The Components Of An Address In A Single Cell
For the moment, you just need to know that the CONCATENATE function allows you to join different text strings into a single one. In this particular case, you use it to join the different components of each address, which you extracted in the step before, into a single cell.
The syntax of the CONCATENATE function is relatively simple: “CONCATENATE(text1,text2,…)”. In other words, the arguments of the CONCATENATE function are simply the text strings you want to join. As shown below, these arguments can also be strings (such as spaces or punctuation marks) surrounded by quotation marks (“”).
This is not too complicated, right?
So let’s go ahead and join all the components of an address in a single cell using the CONCATENATE function.
The form of the final joined addresses is “Street Name ###, City, State Zip Code”. This means that the first address should be River Street 999, Easton, PA 18042.
So how does the formula for this look like?
As you are probably aware, most of the text strings that are joined are in the last few columns (columns U through Y) of the table. These are the columns where the individual items of each address are stored.
However, you need to add spaces ( ) and commas (,). You can do this by using quotation marks (“”).
For example, the formula for cell Z3 is “=CONCATENATE(U3,” “,V3,”, “,W3,”, “,X3,” “,Y3)”.
And Excel returns the expected address.
Copy and paste this formula in all the rows of column Z and you are completely done.
Really!
The table is ready.
Conclusion
Once again…
Congratulations! You are ready to start using the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel.
This Excel Text Functions Tutorial is accompanied by Excel workbooks containing the data and formulas I use above. You can get immediate free access to these example workbooks by subscribing to the Power Spreadsheets Newsletter.
I hope you agree that, despite their relative simplicity, these text functions are very powerful and useful. They are (usually) considered to be among the Excel functions everyone should know due to, among others, how much easier they make life when you are isolating pieces of text or cleaning up large data sets.
If you have completed this tutorial about Excel text formulas, you:
- Have a basic understanding of when you can use the LEFT, RIGHT, MID, LEN, FIND and SEARCH functions in Excel, and what these functions are capable of doing.
- Know the syntax of these formulas in order to be able to apply them in different situations.
You also have a basic understanding of some other functions and tools of Excel, such as the VALUE, ISNUMBER, IF and CONCATENATE functions, filters and sorting commands. I may cover these particular topics in more detail in future Excel tutorials but, using the information included in this guide, you can also start using them immediately.
There are other topics and methods you can learn for purposes of improving your efficiency and productivity when cleaning up data or carrying out similar activities with Excel that I have not covered in this guide. As I mentioned at the beginning of this tutorial about Excel text formulas, there are other methods that can be used to clean up the addresses that appear in the example that I used here.
Books Referenced In This Excel Tutorial
- Bluttman, Ken (2013). Excel Formulas and Functions For Dummies. Indianapolis, IN: John Wiley & Sons Inc.
- Walkenbach, John (2013). Excel 2013 Bible. Indianapolis, IN: John Wiley & Sons Inc.
- Walkenbach, John (2013). Excel 2013 Formulas. Hoboken, NJ: John Wiley & Sons Inc.
Need to retrieve specific characters from a string in Excel? If so, in this guide, you’ll see how to use the Excel string functions to obtain your desired characters within a string.
Specifically, you’ll observe how to apply the following Excel string functions using practical examples:
Excel String Functions Used | Description of Operation |
LEFT | Get characters from the left side of a string |
RIGHT | Get characters from the right side of a string |
MID | Get characters from the middle of a string |
LEFT, FIND | Get all characters before a symbol |
LEFT, FIND | Get all characters before a space |
RIGHT, LEN, FIND | Get all characters after a symbol |
MID, FIND | Get all characters between two symbols |
Excel String Functions: LEFT, RIGHT, MID, LEN and FIND
To start, let’s say that you stored different strings in Excel. These strings may contain a mixture of:
- Letters
- Digits
- Symbols (such as a dash symbol “-“)
- Spaces
Now, let’s suppose that your goal is to isolate/retrieve only the digits within those strings.
How would you then achieve this goal using the Excel string functions?
Let’s dive into few examples to see how you can accomplish this goal.
Retrieve a specific number of characters from the left side of a string
In the following example, you’ll see three strings. Each of those strings would contain a total of 9 characters:
- Five digits starting from the left side of the string
- One dash symbol (“-“)
- Three letters at the end of the string
As indicated before, the goal is to retrieve only the digits within the strings.
How would you do that in Excel?
Here are the steps:
(1) First, type/paste the table below into Excel, within the range of cells A1 to B4 (to keep things simple across all the examples to come, the tables to be typed/pasted into Excel, should be stored in the range of cells A1 to B4):
Identifier | Result |
55555-End | |
77777-End | |
99999-End |
Since the goal is to retrieve the first 5 digits from the left, you’ll need to use the LEFT formula, which has the following structure:
=LEFT(Cell where the string is located, Number of characters needed from the Left)
(2) Next, type the following formula in cell B2:
=LEFT(A2,5)
(3) Finally, drag the LEFT formula from cell B2 to B4 in order to get the results across your 3 records.
This is how your table would look like in Excel after applying the LEFT formula:
Identifier | Result |
55555-End | 55555 |
77777-End | 77777 |
99999-End | 99999 |
Retrieve a specific number of characters from the right side of a string
Wait a minute! what if your digits are located on the right-side of the string?
Let’s look at the opposite case, where you have your digits on the right-side of a string.
Here are the steps that you’ll need to follow in order to retrieve those digits:
(1) Type/paste the following table into cells A1 to B4:
Identifier | Result |
ID-55555 | |
ID-77777 | |
ID-99999 |
Here, you’ll need to use the RIGHT formula that has the following structure:
=RIGHT(Cell where the string is located, Number of characters needed from the Right)
(2) Then, type the following formula in cell B2:
=RIGHT(A2,5)
(3) Finally, drag your RIGHT formula from cell B2 to B4.
This is how the table would look like after applying the RIGHT formula:
Identifier | Result |
ID-55555 | 55555 |
ID-77777 | 77777 |
ID-99999 | 99999 |
Get a specific number of characters from the middle of a string
So far you have seen cases where the digits are located either on the left-side, or the right-side, of a string.
But what if the digits are located in the middle of the string, and you’d like to retrieve only those digits?
Here are the steps that you can apply:
(1) Type/paste the following table into cells A1 to B4:
Identifier | Result |
ID-55555-End | |
ID-77777-End | |
ID-99999-End |
Here, you’ll need to use the MID formula with the following structure:
=MID(Cell of string, Start position of first character needed, Number of characters needed)
(2) Now type the following formula in cell B2:
=MID(A2,4,5)
(3) Finally, drag the MID formula from cell B2 to B4.
This is how the table would look like:
Identifier | Result |
ID-55555-End | 55555 |
ID-77777-End | 77777 |
ID-99999-End | 99999 |
In the subsequent sections, you’ll see how to retrieve your desired characters from strings of varying lengths.
Gel all characters before a symbol (for a varying-length string)
Ready to get more fancy?
Let’s say that you have your desired digits on the left side of a string, BUT the number of digits on the left side of the string keeps changing.
In the following example, you’ll see how to retrieve all the desired digits before a symbol (e.g., the dash symbol “-“) for a varying-length string.
For that, you’ll need to use the FIND function to find your symbol.
Here is the structure of the FIND function:
=FIND(the symbol in quotations that you'd like to find, the cell of the string)
Now let’s look at the steps to get all of your characters before the dash symbol:
(1) First, type/paste the following table into cells A1 to B4:
Identifier | Result |
111-IDAA | |
2222222-IDB | |
33-IDCCC |
(2) Then, type the following formula in cell B2:
=LEFT(A2,FIND("-",A2)-1)
Note that the “-1” at the end of the formula simply drops the dash symbol from your results (as we are only interested to keep the digits on the left without the dash symbol).
(3) As before, drag your formula from cell B2 to B4. Here are the results:
Identifier | Result |
111-IDAA | 111 |
2222222-IDB | 2222222 |
33-IDCCC | 33 |
While you used the dash symbol in the above example, the above formula would also work for other symbols, such as $, % and so on.
Gel all characters before space (for a varying-length string )
But what if you have a space (rather than a symbol), and you only want to get all the characters before that space?
That would require a small modification to the formula you saw in the last section.
Specifically, instead of putting the dash symbol in the FIND function, simply leave an empty space within the quotations:
FIND(" ",A2)
Let’s look at the full steps:
(1) To start, type/paste the following table into cells A1 to B4:
Identifier | Result |
111 IDAA | |
2222222 IDB | |
33 IDCCC |
(2) Then, type the following formula in cell B2:
=LEFT(A2,FIND(" ",A2)-1)
(3) Finally, drag the formula from cell B2 to B4:
Identifier | Result |
111 IDAA | 111 |
2222222 IDB | 2222222 |
33 IDCCC | 33 |
Obtain all characters after a symbol (for a varying-length string )
There may be cases where you may need to get all of your desired characters after a symbol (for a varying-length string).
To do that, you may use the LEN function, which can provide you the total number of characters within a string. Here is the structure of the LEN function:
=LEN(Cell where the string is located)
Let’s now review the steps to get all the digits, after the symbol of “-“, for varying-length strings:
(1) First, type/paste the following table into cells A1 to B4:
Identifier | Result |
IDAA-111 | |
IDB-2222222 | |
IDCCC-33 |
(2) Secondly, type the following formula in cell B2:
=RIGHT(A2,LEN(A2)-FIND("-",A2))
(3) Finally, drag your formula from cell B2 to B4:
Identifier | Result |
IDAA-111 | 111 |
IDB-2222222 | 2222222 |
IDCCC-33 | 33 |
Obtain all characters between two symbols (for a varying-length string)
Last, but not least, is a scenario where you may need to get all of your desired characters between two symbols (for a varying-length string).
In order to accomplish this task, you can apply a mix of some of the concepts we already covered earlier.
Let’s now look at the steps to retrieve only the digits between the two symbols of dash “-“:
(1) First, type/paste the following table into cells A1 to B4:
Identifier | Result |
IDAA-111-AA | |
IDB-2222222-B | |
IDCCC-33-CCC |
(2) Then, type the following formula in cell B2:
=MID(A2,FIND("-",A2)+1,FIND("-",A2,FIND("-",A2)+1)-FIND("-",A2)-1)
(3) And finally, drag your formula from cell B2 to B4:
Identifier | Result |
IDAA-111-AA | 111 |
IDB-2222222-B | 2222222 |
IDCCC-33-CCC | 33 |
Excel String Functions – Summary
Excel string functions can be used to retrieve specific characters within a string.
You just saw how to apply Excel string functions across multiple scenarios. You can use any of the concepts above, or a mixture of the techniques described, in order to get your desired characters within a string.