简体   繁体   中英

Populate cell in Excel wtih substring of another cell

In Excel, I have a CSV in which column A is populated with category like strings, such as:

ABCDE/FGHI/JKL/MNOPQR

I need to auto populate column C with the value of the substring of everything after the last occurrence of the "/". In this example, it would have to be populated with "MNOPQR"

Is there a function that could be used for this? "RIGHT" doesn't seem to do the trick. I don't know what the length of the substring will be in each row, so I definitely have to be able to look for the "/".

If your text is in A4, put this in another cell:

=MID(A4,LEN(LEFT(A4,FIND(CHAR(1),SUBSTITUTE(A4,"/",CHAR(1),LEN(A4)-LEN(SUBSTITUTE(A4,"/",""))))))+1,LEN(A4)-LEN(LEFT(A4,FIND(CHAR(1),SUBSTITUTE(A4,"/",CHAR(1),LEN(A4)-LEN(SUBSTITUTE(A4,"/",""))))))+1)

I think that should work. Thanks to @Jerry for the main part, where it finds the last / in a string.

edit: Why downvote? It works for me: 在此输入图像描述

edit 2: Per @ScottCraner, this is shorter: =MID(A1,SEARCH("}}}",SUBSTITUTE(A1,"/","}}}",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))‌​))+1,LEN(A1))

Here's a bit shorter formula to return the last delimited substring in a string.

=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",99)),99))

Replace the delimiter with 99 spaces, then return the rightmost 99 characters. The leading characters must be spaces also, so TRIM gets rid of them.

A simple formula approach using FilterXML might be:

    =FILTERXML("<items><i>" & SUBSTITUTE(A1,"/","</i><i>") & "</i></items>","//i[position()=last()]")

Profitting from the dynamic features of vers. 2019+ you can change the cell address to a range input, eg A1:A10 allowing output in a so called spill range.

VBA approach

As the VBA tag has been recently added to OP, an obvious VBA approach would be to split the string input and get the last array element via Ubound() :

    Dim tmp As Variant
    tmp = Split("ABCDE/FGHI/JKL/MNOPQR", "/")
    Debug.print tmp(Ubound(tmp))     ' ~~> MNOPQR

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