Home » excel » excel – How can i extract the first and last number from a string separated by semicolons?

excel – How can i extract the first and last number from a string separated by semicolons?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a string in cell A1

7;19;28;42;

I would like to extract the first and last numbers. I have used this to extract the first number successfully

=IF(ISERROR(LEFT($a1,FIND(";",$a1)-1)),$a1,LEFT($a1,FIND(";",$a1)-1))

I have used this to try and extract the last number

=IF(ISERROR(RIGHT($a1,FIND(";",$a1)+1)),$a1,RIGHT($a1,FIND(";",$a1)+1))

However the problem I am finding is that the last number often has the semicolon before and/or after it. Is there anyway just to extract the numbers?

How to&Answers:

Shorter version of getting the first:

=LEFT($A1,FIND(";",$A1 & ";")-1)

To get the last:

=SUBSTITUTE(MID($A1,FIND("}}}",SUBSTITUTE(";" & $A1,";","}}}",LEN(";" & $A1)-LEN(SUBSTITUTE(";" & $A1,";",""))-1)),LEN($A1)),";","")

enter image description here

Answer:

Extract first number :

=-LOOKUP(1,-LEFT(A1,ROW($1:$9)))

Extract last number :

=-LOOKUP(1,-RIGHT(TRIM(SUBSTITUTE(A1,";"," ")),ROW($1:$9)))

Answer:

Alternative solutions just for FBF Friday-Brainstorm-Fun 🙂

First number

=FILTERXML("<t><s>"&SUBSTITUTE(A1,";","</s><s>")&"</s></t>","t/s[1]")

Last number:

=--TRIM(RIGHT(SUBSTITUTE(LEFT(A1,LEN(A1)-1),";",REPT(" ",500)),100))

Solutions

Answer:

Just for my own amusement, one that gets the last number in all these cases:

=TRIM(RIGHT(SUBSTITUTE(TRIM(SUBSTITUTE(A1,";"," "))," ",REPT(" ",LEN(A1))),LEN(A1)))

enter image description here