Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to extract remaining string after last backslash

I need an Excel function that can extract a string after last \ from a path and if no \ found then take the whole string. For example:

D:\testing\rbc.xls                     output will be   rbc.xls
D:\home\testing\test1\script1.sql      output will be   script.sql
script 3.txt                           output will be   script 3.txt
like image 434
user664481 Avatar asked Dec 12 '15 09:12

user664481


People also ask

How do I extract a string after the last comma in Excel?

Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button. Tips: (1) If you want to extract text before or after comma, you can change " " to ",".

Which function is used to extract the string from the left?

SQL Server LEFT() Function The LEFT() function extracts a number of characters from a string (starting from left).

How do you get a string after a specific character in Excel?

To get text following a specific character, you use a slightly different approach: get the position of the character with either SEARCH or FIND, subtract that number from the total string length returned by the LEN function, and extract that many characters from the end of the string.


1 Answers

1.Change all the "\" to spaces, the number of spaces is determined by the number of characters in the cell

2.Use the right function to extract the right of the string based on the number of characters in the cell.

3.Use the trim function to remove the spaces.

enter image description here

Your results will be.

enter image description here

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

As suggested, one way to do this without formulas or vba would be to use "Find/Replace". Hit Ctrl & "H" keys and do the following.

Find what *\ and replace with nothing

enter image description here

The VBA code for that would be

Sub ReplaceIt()
    Columns("A").Replace What:="*\", Replacement:="", SearchOrder:=xlByColumns, MatchCase:=True
End Sub
like image 65
Davesexcel Avatar answered Oct 01 '22 00:10

Davesexcel