This is a series on what Google Spreadsheets can do that Excel can’t.
SPLIT(string, delimiter) splits a string using a delimiter. So if you have "one,two,three,four" in cell A1, you could split that into 4 cells using =SPLIT(A1,",")
That’s similar to Data > Text to Columns, except that if the original data changed, Text to Columns does not revise the output. SPLIT can give you dynamic text-to-columns. This is pretty useful when processing text data, in three ways:
- You retain the original data
- You don’t need to re-apply Text to Columns. Extending the formula will work (and that’s quicker)
- It’s dynamic. If the data changes, your split changes
Since SPLIT returns an array, you can do a bunch of useful things with it.
=COUNTA(SPLIT(A1," "))
gives you the number of words in a string
=SUM(SPLIT(A1,","))
sums up a comma separated list. "1,2,3,4" is added up to 10.
=ARRAYFORMULA(SUM(LEN(SPLIT(A1,","))))
sums up the word lengths. So "one,two,three,four" splits into 4 words of length 3,3,5,4 each, which adds up to 15.
The ability to join and split also lets you sort by multiple keys. For example, say you had income by country and product. You want to show it sorted by Country & Product. You also want to show it by Product & Country.
So first take the data sorted by Country and Product.
Now, in column E, create a key that’s sorted by Product and then by Income. Type
=SORT(ARRAYFORMULA(B2:B10&":"&A2:A10&":"&C2:C10))
… in cell E2. That will give you all the data in one cell, sorted by Product and then by Country. Now, just split the data, as shown here.
Note: You could have done the whole thing using one formula:
=ARRAYFORMULA(SPLIT(SORT(ARRAYFORMULA(B2:B10&":"&A2:A10&":"&C2:C10)),":"))
But for some reason, this doesn’t seem to show the first row properly. No idea why.
Can I do that in Excel?
Well, not really. You’re best off creating a user-defined function to duplicate the SPLIT function.
Thanks Anand. This is very useful.
Thanks Anand. This is very useful.
Cant “mid” with “find” and “len” replace “split”? Even though its a round about way i think it can still do it.
@Sridhar: Yes, this can be done, within reason. Chandoo has a post about Splitting text in excel using formulas.
It can be done in excel – we used excel to split up a sentence field into constituent words – but it is a bit roundabout – search for spaces or commas, count, split by count of space/comma