Google Search

Custom Search

How to find the position of last of the multiple characters in a cell

Let's look at the example here. There are cells that contain something similar to 

drive1\folder1\file1023
drive2\folder2\subfolder2\file256
drive10\file34
..
..
..


The text after the last "/" is to be extracted, the result should be,

file1023
file256
file34
..
..

To do that you need position of the last "/". FIND function alone is not use because of two unknowns, one how many "/" s are there and second what is the "start number" to use.

But it is possible using RIGHT, FIND, SUBSTITUTE and LEN.
Let's say drive1\folder1\file1023 is in cell A1.

The formula to get file1023 will be

RIGHT(A1,LEN(A1)-FIND("#",SUBSTITUTE(A1,"/","#",LEN(A1)-LEN(SUBSTITUTE(A1,"/","")))))

Note '#' can be any character.

Let's analyse the formula to see how it works. The formula works using [instance_num]  (W
hich is not available in FIND function)  of the SUBSTITUTE function and substituting "\" with "" (or nothing).  

SUBSTITUTE(text, old_text, new_text, [instance_num])

Lets look at the formula from right to left.

LEN(SUBSTITUTE(A1,"\","")) will give number of characters after replacing "\" with "".

LEN(A1) will give number of characters before any change.

The difference is the number of "\" in the cell, in this example 2.

SUBSTITUTE(A1,"\","#",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))) will replace only the last "\" with "#" using 2 as the  [instance_num ]. Now "#" is in the last "\" position.

Now using FIND the position of "#" can be found, in this example 15.

Using RIGHT formula with identified position of the "#" it is possible to extract file 1023.


LEN(SUBSTITUTE(A1,"\",""))
21
LEN(A1)
23
LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))
2
SUBSTITUTE(A1,"\","#",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))
drive1\folder1#file1023
FIND("#",SUBSTITUTE(A1,"\","#",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))
15
RIGHT(A1,LEN(A1)-FIND("#",SUBSTITUTE(A1,"/","#",LEN(A1)-LEN(SUBSTITUTE(A1,"/","")))))
file1023



Please let me know if you have any concerns or questions.

1 comment:

  1. Hi, thanks for this excel tutorial. I Am Learning Excel Now, and this material make me understand more

    ReplyDelete

 

blogger templates | Make Money Online

Google Analytics Alternative ExpiresDefault access plus 1 year