Text split to array
To split text with a delimiter and transform the result into an array, you can use the FILTERXML function with help from the SUBSTITUTE and TRANSPOSE functions. In the example shown, the formula in D5 is:
Note: FILTERXML is not available in Excel on the Mac, or in Excel Online.
Note: I learned this trick from Bill Jelen in a MrExcel video.
Excel does not have a function dedicated to splitting text to an array, similar to the PHP explode function, or Python split method. As a workaround, you can use the FILTERXML function, after first adding XML markup to the text.
In the example shown, we have several comma-delimited text strings like this:
The goal is to split the information into separate columns using the comma as a delimiter.
The first task is to add XML markup to this text, so that it can be parsed as XML with the FILTERXML function. We are going to arbitrarily make each field in the text a
The result from SUBSTITUTE is a text string like this:
To ensure well-formed XML tags and to wrap all
This yields a text string like this (line breaks added for readability)
"<x> <y>Jim</y> <y>Brown</y> <y>33</y> <y>Seattle</y> <y>WA</y> </x>"
This text is delivered directly to the FILTERXML function as the xml argument, with an Xpath expression of "//y":
Xpath is a parsing language and "//y" selects all
Because we want a horizontal array in this instance, we wrap the TRANSPOSE function around FILTERXML:
The result is a horizontal array like this:
Extract nth item
The formula can be extended with the INDEX function to extract the nth item. For example, to extract just the age, you can use INDEX with a row number of 1 and column number of 3:
=INDEX(D5#,1,3) // age only
Or, in an all-in-one formula: