Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate string split function in Excel formula

I am trying to split a string in an excel formula, something like I can do in many programming languages, e.g.

string words = "some text".split(' ');

The problem is that I can't be sure that there is more than one word in the cell. If I try to use the FIND() or SEARCH() functions, they return #VALUE if there is not space. Is there any easy way to split the string so that it returns the individual words (or even better, so that it returns either the first word or all the other words)?

like image 365
a_m0d Avatar asked Jun 23 '09 08:06

a_m0d


People also ask

Can you split a string in Excel?

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.

What is split formula in Excel?

Description. The Microsoft Excel SPLIT function will split a string into substrings based on a delimiter. The result is returned as an array of substrings. The SPLIT function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel.


3 Answers

A formula to return either the first word or all the other words.

=IF(ISERROR(FIND(" ",TRIM(A2),1)),TRIM(A2),MID(TRIM(A2),FIND(" ",TRIM(A2),1),LEN(A2))) 

Examples and results

Text                  Description                      Results                        Blank                        Space  some                  Text no space                some some text             Text with space                  text  some                 Text with leading space          some some                  Text with trailing space         some some text some text   Text with multiple spaces        text some text 

Comments on Formula:

  • The TRIM function is used to remove all leading and trailing spaces. Duplicate spacing within the text is also removed.
  • The FIND function then finds the first space
  • If there is no space then the trimmed text is returned
  • Otherwise the MID function is used to return any text after the first space
like image 152
Robert Mearns Avatar answered Sep 20 '22 18:09

Robert Mearns


The following returns the first word in cell A1 when separated by a space (works in Excel 2003):

=LEFT(A1, SEARCH(" ",A1,1)) 
like image 30
RobS Avatar answered Sep 22 '22 18:09

RobS


=IFERROR(LEFT(A3, FIND(" ", A3, 1)), A3)

This will firstly check if the cell contains a space, if it does it will return the first value from the space, otherwise it will return the cell value.

Edit

Just to add to the above formula, as it stands if there is no value in the cell it would return 0. If you are looking to display a message or something to tell the user it is empty you could use the following:

=IF(IFERROR(LEFT(A3, FIND(" ", A3, 1)), A3)=0, "Empty", IFERROR(LEFT(A3, FIND(" ", A3, 1)), A3))
like image 34
James Avatar answered Sep 20 '22 18:09

James