## Excel string parsing

Parsing strings isn’t the easiest thing to do in Excel 2007 or Excel 2008 but it is an important thing to know how to do. You’ll learn how to parse a set names into first, middle and last names. You can find the data set for these examples at the bottom of the blog post.

** Parsing the left substring** ↓

This shows you how to parse a left substring from a text cell in Microsoft Excel.

There are two built in functions that let you parse dynamic substrings from the left of a string. They are the `LEFT`

and `FIND`

functions. The `LEFT`

function actually does the parsing but the `FIND`

function lets you dynamically find a delimiting character, like a space.

Assuming cell `A1`

holds the value of *Joseph F. Smith*, you can parse *Joseph* by using a static value of seven for the first white space in the string. This works because each character maps to a string, and strings start with the number one. The `LEFT`

function supports three formal parameters, the first is the string you’re parsing, the second is where to stop, and the third is where to start. The position you start at is assumed to be one, which makes the last parameter an optional parameter. You only provide the *start with* parameter when you want to start someplace other than the left most position.

This is the simplest use of the `LEFT`

function with its two required parameters.

=LEFT(A1,7) |

It returns the substring *Joseph* from the string *Joseph F. Smith*, which is stored in cell `A1`

.

The simplest solution merely illustrates a concept. You’re not going to do this unless you have a bit more data. Naturally, the list of first names have different lengths in the real world. You must include the `FIND`

function inside the call to the `LEFT`

function to make your function dynamic. The `FIND`

function lets you capture the position of a white space delimiter.

The `FIND`

function supports three formal parameters, the first is the substring you’re looking for in the string, the second is the string to search, and the third is where to start. Like the `LEFT`

function, the position you start at is assumed to be one, which makes the last parameter an optional parameter. You only provide the *start with* parameter when you want to start someplace other than the left most position. While not required, you should consider always providing the *start with* parameter to the `FIND`

function.

The following shows you how to use the `FIND`

function to locate the first occurrence of a white space in a string stored in cell `A1`

.

=FIND(" ",A1,1) |

You can now make you’re `LEFT`

function dynamic by putting the `FIND`

function inside it. As qualified, the `FIND`

function looks for the first occurrence of a white space, and returns it into the second parameter of the `LEFT`

function. The sample formula now has a nested function, which makes it a *mega formula* in Microsoft Excel parlance. The following sample also includes the optional *start with* parameter for clarity.

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

The problem with the foregoing solution occurs when a name in the list doesn’t have a middle initial or name, or last name. The lack of a second name means that there won’t be any white space between to substrings in the base string. This situation causes the logic to fail because the `FIND`

function returns a `#VALUE!`

error when it can’t find a white space in the string.

You can prevent the error by wrapping the nested `FIND`

function and `LEFT`

function inside two `IFERROR`

functions. This `IFERROR`

function returns the positional value of the `FIND`

function, or an alternative copy of the base string.

=IFERROR(FIND(" ",A1,1),A1) |

This type of logic inside the `LEFT`

function causes the `LEFT`

function to fail when the base string is returned to it instead of a position number. You must wrap the `LEFT`

function inside another `IFERROR`

function to guarantee that you don’t throw an error. This also lets you return the base string as the valid substring when appropriate.

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

Assuming you put this formula in cell `C1`

, you could copy this relative reference formula down in a column. It would require that you had a list of full names that followed a first name, white space, middle initial or name, white space, and last name in the `A`

column.

** Parsing the right substring** ↓

This shows you how to parse a right substring from a text cell in Microsoft Excel.

There are three built in functions that let you parse dynamic substrings from the right of strings. They are the `RIGHT`

and `FIND`

functions that you may have covered when reading how to parse from the left. The `LEN`

function is the other function, and it lets you find the length of a string. Together these functions lets you find the length of a substring on the right.

The `RIGHT`

function actually does the parsing but the `FIND`

and `LEN`

functions let you dynamically find where to cut a substring out of a base string. This example continues to use the string *Joseph F. Smith*.

The `RIGHT`

function has only two required parameters. The first parameter is the string that you’re parsing. The second parameter is the length of the substring. If you inspect the string, *Smith* is only five characters long. A static call to the `RIGHT`

function is shown below.

=RIGHT(A1,5) |

It returns the substring *Smith* from the string *Joseph F. Smith*, which is stored in cell `A1`

.

Like the `LEFT`

function example, this static approach to parsing merely illustrates a concept. You’re not going to do this with real data because the list of first names have different lengths in the real world. You must include the `FIND`

function twice inside your call to the `RIGHT`

function because you’re parsing the string based on the second occurrence of a white space in the string. While the `FIND`

function lets you dynamically capture the position of the white space delimiter, a nested `FIND`

function lets you capture the correct *start with* parameter value. That value is one position after the first occurrence of a white space.

The `FIND`

function supports three formal parameters, the first is the substring you’re looking for in the string, the second is the string to search, and the third is where to start. Like the `LEFT`

function, the position you start at is assumed to be one, which makes the last parameter an optional parameter. You only provide the *start with* parameter when you want to start someplace other than the left most position. While not required, you should consider always providing the *start with* parameter to the `FIND`

function. It improves readability.

The following shows how to use a `FIND`

function to locate the position one beyond where the first occurrence of a white space is found in a string.

=FIND(" ",A1,1)+1 |

If you pass the preceding `FIND`

function call as the *start with* parameter to another `FIND`

function, you can locate the second occurrence of a white space in a string stored in cell `A1`

. Nesting function calls inside functions creates what are known as *mega formulas* in Excel. The following demonstrates a *mega formula* to find the second instance of a white space in a string.

=FIND(" ",A1,FIND(" ",A1,1)+1) |

This returns the value of ten. You now know where to start but not the length of the substring on the right. You first need to find the length of the total string. You use the `LEN`

function to find that, like the following.

=LEN(A1) |

The `LEN`

function returns fifteen. You can calculate the length of the substring as five by subtracting the position of the second white space from the length of the string. While you could inspect that value in this one cell, you can’t do that when there are one hundred or one hundred thousand names in a list. The way to dynamically capture the right hand side substring length is shown below.

=LEN(A1)-FIND(" ",A1,FIND(" ",A1,1)+1) |

Now that you know how to capture the length of the substring, you can create a larger *mega forumla* to parse the substring on the right from the base string. The working example follows below.

=RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND(" ",A1,1)+1)) |

The problem with the foregoing solution is that it fails when one of the names in the list doesn’t have a middle initial or name. The failure occurs because there would only be one white space in the base string, and the logic expects two. The `FIND`

function looking for the second white space returns a `#VALUE!`

error.

You can prevent this error by wrapping the nested `FIND`

function calls and `RIGHT`

function with calls with an `IFERROR`

function. An `IFERROR`

function returns the positional value of the `FIND`

function, or an alternate value, like a fresh copy of the base string.

=IFERROR(RIGHT(A1,LEN(A1)-IFERROR(FIND(" ",A1,FIND(" ",A1)+1),FIND(" ",A1))),A1) |

The `IFERROR`

function inside the `RIGHT`

function can cause the `RIGHT`

function to fail when a base string is returned instead of a position number. Therefore, you must also wrap the `RIGHT`

function inside another `IFERROR`

function to avoid an error. This guarantees the return of the base string as a valid substring.

Assuming you put this formula in cell `C1`

, you could copy this relative reference formula down in a column. It would require that you had a list of full names that followed a first name, white space, middle initial or name, white space, and last name in the `A`

column.

** Parsing the middle substring** ↓

This shows you how to parse a middle substring from a text cell in Microsoft Excel.

There are three built in functions that are required to let you parse dynamic substrings from the middle of base strings. Two of them are the `MID`

and `FIND`

functions. You’ve seen how to use the `FIND`

function in the left and right parsing examples of this blog. The third function is the `IFERROR`

function, which is required when a middle string doesn’t exist. There are two more functions that let you trap for the possibility of a single base string. They are the `IF`

and `ISNUMBER`

functions.

The `MID`

function takes three required parameters. The first is the text value or cell reference, the second is the *start with* value, and the third is the length of the substring. Dynamic substrings require you to bracket them, which means you need to find their beginning and ending positions and measure their length.

This basic idea means you parse the middle string from a set of three strings by finding their delimiters. As in the other examples, you’ll work with the string *Joseph F. Smith* as a base string. First, you find the first character of the middle string. You do this by finding the position of the first delimiting white space with a `FIND`

function, and then you add one to the returned result value. The example is below.

=FIND(" ",A1,1)+1 |

Next, you find the position of the second delimiter. This requires that you create what is known as a *mega formula*, which you create by nesting one or more formulas in another. You can use the following formula to do that.

=FIND(" ",A1,FIND(" ",A1)+1) |

You can then calculate the length of the middle string by subracting the first result from the second one, as shown below:

=FIND(" ",A1,FIND(" ",A1)+1)-FIND(" ",A1)-1 |

After you’ve mastered those formulas, you need to create a *mega formula* with the `MID`

function. This doesn’t have any error trapping yet, so it is very dependent on data that contains three substrings separated by white spaces.

=MID(A1,FIND(" ",A1)+1,FIND(" ",A1,FIND(" ",A1)+1)-FIND(" ",A1)-1) |

The preceding function returns *F.*. If the middle string were a middle name, it would return the middle name. It fails when you have a base string that lacks three substrings. You need to wrap the nested `FIND`

function and `MID`

function inside two respective `IFERROR`

functions. An `IFERROR`

function returns the positional value of the `FIND`

function, or an alternate value, like a fresh copy of the base string or a numeric equivalent. The following example uses an `IFERROR`

function call to substitutes a zero value because the absence of a second white space means there isn’t a middle string.

=MID(A5,FIND(" ",A5)+1,IFERROR(FIND(" ",A5,FIND(" ",A5)+1)-FIND(" ",A5)-1,0)) |

A second `IFERROR`

wrapping the `MID`

function lets you return a null value for middle name when there is only one name in the base string, like *Joseph*.

=IFERROR(MID(A5,FIND(" ",A5)+1,IFERROR(FIND(" ",A5,FIND(" ",A5)+1)-FIND(" ",A5)-1,0)),"") |

Assuming you put this formula in cell `D1`

, you could copy this relative reference formula down in a column. It would require that you had a list of full names that followed a first name, white space, middle initial or name, white space, and last name in the `A`

column.

** Data set** ↓

A list of native strings, parsed first, middle, and last names, and concatenated names.

Original Name First Name Middle Name Last Name Resorted Name Joseph Smith Joseph Smith Smith, Joseph Brigham Young Brigham Young Young, Brigham John Taylor John Taylor Taylor, John Wilford Woodruff Wilford Woodruff Woodruff, Wilford Lorenzo Snow Lorenzo Snow Snow, Lorenzo Joseph F. Smith Joseph F Smith Smith, Joseph F Heber J. Grant Heber J Grant Grant, Heber J George Albert Smith George Albert Smith Smith, George Albert David O. Mckay David O Mckay Mckay, David O Joseph Fielding Smith Joseph Fielding Smith Smith, Joseph Fielding Harold B. Lee Harold B Lee Lee, Harold B Spencer W. Kimball Spencer W Kimball Kimball, Spencer W Ezra Taft Benson Ezra Taft Benson Benson, Ezra Taft Howard W. Hunter Howard W Hunter Hunter, Howard W Gordon B. Hinckley Gordon B Hinckley Hinckley, Gordon B Thomas S. Monson Thomas S Monson Monson, Thomas S |

I thought this has helped me alot to understand Parsing and it is simple and easy to understand. The examples helped me and I feel like I can do this a lot better now.

Kyle Shurtliff6 Mar 09 at 9:02 am

Thanks – clear, used parsing in Lotus 123, just catching up !!

Mike Egerton1 Jun 09 at 12:42 am

I found this page to be immensely useful in figuring out how to parse strings from the right. Logical and well thought out.

Bob Jensen11 Mar 10 at 3:08 pm

This was clear and consise. Thank you!

Tony Grace21 Oct 10 at 11:39 am

Very well written, thank you. This article saved me a ton of time!

Brian S8 Nov 10 at 11:46 am

A shorter version for the left substring parsing:

=LEFT(A1,IFERROR(FIND(” “,A1),LEN(A1)))

Mc20 Jun 11 at 9:30 am

Great post! helped me quickly.

Thank you so much.

Pal Ram30 Aug 11 at 7:30 pm

This was a great in depth tutorial that really guided me through why you needed the

`IFERROR`

and not just the`FIND`

function. Thank You.Izzy Haggen20 Sep 11 at 12:06 pm

Thanks – I used your approach (nested FIND functions) to parse the file name out of a file path (delimiting on “/”) without using VB.

This approach only works if you have a reasonably low limit to the number of segments you’re expecting. In my case, using the pasted formula below, the limit is six delimiters (folders). I’ll include a copy in case it helps someone reading these comments ðŸ™‚

To parse out the last value in a delimited list of 6 or fewer values, use this formula but replace A1 with the location of your string, and “/” with your actual delimiter:

Jeff Geurts20 Nov 11 at 4:47 pm

Very useful. Thanks for posting. @JeffGeurts: It was easy for me to adapt your very long formula to grab each Web Service name from a list of long URLs. I didn’t have an extension to strip so I removed the -4 at the end. I also updated the start position from 1 to 50 in each location that had “, A1, 1)” which is a handy way to reduce the length if you have a common base URL that you can just discard.

zephans6 Sep 12 at 10:58 pm

Incredibly helpful. Thanks for this post.

mkubica26 Oct 12 at 7:23 am

Thank you *so much* for this post. I have trouble parsing strings myself, and a student I was tutoring needed to learn it for a test. I used your page as instructional material. You are just excellent at explaining such difficult logical concepts in a way the student could learn them for the test and apply them in real time. I really appreciate the work you put into the post. You have taught me a lot. Thanks again!

Monika24 Apr 13 at 4:14 am

Hi,

I have been trying to understand this subject and not having one ounce of programming skill I am still struggling despite the detialed explanation.

I simply want to parse this string;

1. Burlington Gardens 100

so that;

1. goes to cell A1, Burlington Gardens goes to cell A2 and 100 goes to cell A3

The 2 text words (Burlington Gardens) may sometimes be one word or three, but a space character is always the delimiter.

CNXTim24 Aug 13 at 7:00 pm

Mormon prophets! Woo-woo!

Greg18 Dec 13 at 12:55 pm