简体   繁体   中英

Excel: copy rule before last /

I try copy text before last /

Sample:

  • 14501/30305
  • 14501/30305
  • 14507/39756
  • 15936/15943/15954
  • 14507/15940/30405
  • 15936/15943/15980

Result:

  • 14501
  • 14501
  • 14507
  • 15936/15943
  • 14507/15940
  • 15936/15943

I try somelike this but no work in my excel. Return error invalid formula.

=IF(ISERROR(FIND(" ",A2,FIND(" ",A2,1)+1)),A2,LEFT(A2,FIND(" ",A2,FIND(" ",A2,1)+1))) 

Everytime I get issue This formula is invalid: 在此处输入图像描述

Not a clear answer, but you may go this way (I used cell A1 for my example):

  1. = LEN(A1)-LEN(SUBSTITUTE(A1;"/";"") - This will count how many / you have in A1 cell
  2. Then you may use this count in SUBSTITUTE function:
    = SUBSTITUTE(A1;"/";"#";LEN(A1)-LEN(SUBSTITUTE(A1;"/";"")))
    in order to replace last / occurrence with some other unique symbol - # in my example
  3. Use FIND to locate this new symbol starting search from 1st symbol:
    =FIND("#"; SUBSTITUTE(A1;"/";"#";LEN(A1)-LEN(SUBSTITUTE(A1;"/";""))); 1)
    it will give you localtion of # , ie location of last / in initial string.
  4. Use this location for LEFT function:
    =LEFT(A1; FIND("#"; SUBSTITUTE(A1;"/";"#";LEN(A1)-LEN(SUBSTITUTE(A1;"/";""))); 1)-1)

You could use multiple ways, for example:


MATCH() the last occurence of the forward slash:

=LEFT(A1,MATCH(2,1/(MID(A1,SEQUENCE(LEN(A1)),1)="/"))-1)

Or if one does not have Microsoft365:

=LEFT(A1,MATCH(2,1/(MID(A1,ROW(A1:INDEX(A:A,LEN(A1))),1)="/"))-1)

SUBSTITUTE() the last occurence of the forward slash:

=LEFT(A1,FIND("|",SUBSTITUTE(A1,"/","|",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))-1)

TEXTJOIN() any other than last part:

=TEXTJOIN("/",,FILTERXML("<t><s>"&SUBSTITUTE(A1,"/","</s><s>")&"</s></t>","//s[position()<last()]"))

在此处输入图像描述


Edit:

If this is done in GS for now, you can use REGEXEXTRACT() inside and INDEX() :

=INDEX(IF(A2:A="",,REGEXEXTRACT(A2:A,"(^.*)\/")),,)

This will spill all your answers down in the same column.

Just throwing in another version of SUBSTITUTE|RIGHT in the ring as others have already demonstrated.

=SUBSTITUTE(A2,"/"&TRIM(RIGHT(SUBSTITUTE(A2,"/",REPT(" ",99)),99)),"")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM