Simulate string split function in Excel formula

后端 未结 8 1713
难免孤独
难免孤独 2020-12-09 09:15

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(\' \');


        
相关标签:
8条回答
  • 2020-12-09 09:46

    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
    0 讨论(0)
  • 2020-12-09 09:46
    =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))
    
    0 讨论(0)
提交回复
热议问题