+ Reply to Thread
Results 1 to 8 of 8

Find the last occurrence of a character in a string

  1. #1
    Forum Contributor
    Join Date
    05-19-2004
    Location
    United States
    MS-Off Ver
    Office XP and Office 2003
    Posts
    127

    Find the last occurrence of a character in a string

    I would like to find the position of the last occurrence of a character in a string. For example, I have a string with the following: "c:\wwgpeTool\bids\Test File.GP$". I would like to find out the position of the last '\' in the string because I want to pull the filename, 'Test File.GP$' into a variable. I would like to avoid writing code to do this. Are there some Excel functions I can use for this?

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    This formula pulls the filename from the string, it doesn't return the position of the last "\", though it can be done.
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try

    =RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"\","~",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  4. #4
    Forum Contributor
    Join Date
    05-19-2004
    Location
    United States
    MS-Off Ver
    Office XP and Office 2003
    Posts
    127

    It worked!

    Thank you!

  5. #5
    Registered User
    Join Date
    11-16-2012
    Location
    Camas, WA
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Find the last occurrence of a character in a string

    It worked in Excel 2007 too.

  6. #6
    Banned User!
    Join Date
    10-14-2006
    Posts
    1,211

    Re: Find the last occurrence of a character in a string

    =trim(right(substitute(a1,"\",rept(" ",99)),99))

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Find the last occurrence of a character in a string

    To answer your question directly, this returns the position of the last occurrence of "\" in your string "c:\wwgpeTool\bids\Test File.GP$"
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    "|" (pipe) can be any character unlikely to be found in your string.

    I would suggest avoiding "~" (tilde), it can in certain circumstances, cause Excel to crash.

  8. #8
    Registered User
    Join Date
    08-31-2013
    Location
    Watsonville, CA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Find the last occurrence of a character in a string

    Quote Originally Posted by Teethless mama View Post
    =trim(right(substitute(a1,"\",rept(" ",99)),99))
    OMG, I just registered on this site to comment about this formula.

    This is just absolutely brilliant! Just brilliant.

    Dumb me, I would have gone blithely down the road illustrated by the following thinking.

    These are for illustration purposes only, they are not formulas I've written nor tested for this project. Only to illustrate the "literal" way (compared to above) that I have always thought about writing excel formulas.

    =RIGHT(A1,LEN(A1)-SEARCH("@",SUBSTITUTE(A1," ","@",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))
    =MID(MID(MID(SUBSTITUTE(A6," ","^",3),1,256),FIND("^",SUBSTITUTE(A6," ","^",3)),256),2,FIND(" ",MID(MID(SUBSTITUTE(A6," ","^",3),1,256),FIND("^",SUBSTITUTE(A6," ","^",3)),256))-2)
    =SEARCH("^^",SUBSTITUTE(A1,"\","^^",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))
    =LOOKUP(2,1/(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)="\"),ROW(INDIRECT("1:"&LEN(A1))))

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1