+ Reply to Thread
Results 1 to 4 of 4

Excel 2007 : Parse string of text from one cell

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2006
    Location
    Alberta, Canada
    Posts
    15

    Parse string of text from one cell

    I have the path for some of my data in column A and it ends with a back slash "\". I have been trying to find a way to start at the right of a cell and backtrack to the second occurrence of this and extract the text up to right until you hit the next/last occurrence of it. I have attached a sample spreadsheet with sample data and the desired result.

    I have spent a few hours trying to nail this one down and searched all the help and forums that I can find.

    Any assistance would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,952

    Re: Parse string of text from one cell

    Here's a small user function to search from the end of the string:
    USAGE:
    Given a path in cell B4, then in C4: "=GetFolder(B4)"
    Option Explicit
    Public Function GetFolder(ByVal CellAddr As Range) As String
        Dim StrLen      As Long, _
            kounter     As Long, _
            TestPath    As String, _
            Ptr         As Long
            TestPath = CellAddr.Value
            StrLen = Len(TestPath) - 1
            For kounter = StrLen To 1 Step -1
                If Mid(TestPath, kounter, 1) = "\" Then Exit For
                Ptr = Ptr + 1
            Next kounter
            GetFolder = Mid(TestPath, kounter + 1, Ptr)
    End Function
    Ben Van Johnson

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Parse string of text from one cell

    An approach using native formulae:

    C4:
    =TRIM(MID(SUBSTITUTE(B4,"\",REPT(" ",100)),100*(LEN(B4)-LEN(SUBSTITUTE(B4,"\",""))-(RIGHT(B4,1)="\")),100))
    An alternative UDF:

    Function LastFolder(rngPath As Range)
    LastFolder = Split(rngPath, "\")(Len(rngPath) - Len(Replace(rngPath, "\", "")) - Abs(Right(rngPath, 1) = "\"))
    End Function
    Called from cell

    C4: =LastFolder(B4)
    Last edited by DonkeyOte; 02-20-2009 at 05:05 AM.

  4. #4
    Registered User
    Join Date
    05-23-2006
    Location
    Alberta, Canada
    Posts
    15

    Solved Re: Parse string of text from one cell

    Thank you both. Your help is greatly appreciated.

+ 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