+ Reply to Thread
Results 1 to 7 of 7

Checking a String's last character

  1. #1
    Ren
    Guest

    Checking a String's last character

    I want to get a folder path from a a cell and have a check function where the
    function would check if the string's last character is "\"

    So that if the String is "C:\folderpath" the string would return
    "C:\folderpath\" and leave it as is if the String is "C:\folderpath\"

    What VBA function would allow me to do so?

  2. #2
    Dave Peterson
    Guest

    Re: Checking a String's last character

    dim myPath as string
    mypath = "c:\folderpath"
    if right(mypath,1) <> "\" then
    mypath = mypath & "\"
    end if



    Ren wrote:
    >
    > I want to get a folder path from a a cell and have a check function where the
    > function would check if the string's last character is "\"
    >
    > So that if the String is "C:\folderpath" the string would return
    > "C:\folderpath\" and leave it as is if the String is "C:\folderpath\"
    >
    > What VBA function would allow me to do so?


    --

    Dave Peterson

  3. #3
    Graham Whitehead
    Guest

    Re: Checking a String's last character

    You need to use the Right function, here is a short example

    Dim strTest As String
    Dim strExample As String

    strTest = Range("E1").Value
    strExample = Right(strTest, 1)

    If strExample <> "\" Then
    strTest = strTest & "\"
    End If

    "Ren" <[email protected]> wrote in message
    news:[email protected]...
    >I want to get a folder path from a a cell and have a check function where
    >the
    > function would check if the string's last character is "\"
    >
    > So that if the String is "C:\folderpath" the string would return
    > "C:\folderpath\" and leave it as is if the String is "C:\folderpath\"
    >
    > What VBA function would allow me to do so?




  4. #4
    Ron Rosenfeld
    Guest

    Re: Checking a String's last character

    On Wed, 2 Aug 2006 07:36:02 -0700, Ren <[email protected]> wrote:

    >I want to get a folder path from a a cell and have a check function where the
    >function would check if the string's last character is "\"
    >
    >So that if the String is "C:\folderpath" the string would return
    >"C:\folderpath\" and leave it as is if the String is "C:\folderpath\"
    >
    >What VBA function would allow me to do so?


    Just another method:

    path = "C:\folderpath"
    Replace(path & "\", "\\", "\")



    --ron

  5. #5
    Dave Peterson
    Guest

    Re: Checking a String's last character

    I'd watch out for UND paths, though:

    Dim myPath As String
    myPath = "\\share\folderpath"
    myPath = Replace(myPath & "\", "\\", "\")



    Ron Rosenfeld wrote:
    >
    > On Wed, 2 Aug 2006 07:36:02 -0700, Ren <[email protected]> wrote:
    >
    > >I want to get a folder path from a a cell and have a check function where the
    > >function would check if the string's last character is "\"
    > >
    > >So that if the String is "C:\folderpath" the string would return
    > >"C:\folderpath\" and leave it as is if the String is "C:\folderpath\"
    > >
    > >What VBA function would allow me to do so?

    >
    > Just another method:
    >
    > path = "C:\folderpath"
    > Replace(path & "\", "\\", "\")
    >
    > --ron


    --

    Dave Peterson

  6. #6
    Dave Peterson
    Guest

    Re: Checking a String's last character

    er, UNC paths....

    Darn fingers!

    Dave Peterson wrote:
    >
    > I'd watch out for UND paths, though:
    >
    > Dim myPath As String
    > myPath = "\\share\folderpath"
    > myPath = Replace(myPath & "\", "\\", "\")
    >
    > Ron Rosenfeld wrote:
    > >
    > > On Wed, 2 Aug 2006 07:36:02 -0700, Ren <[email protected]> wrote:
    > >
    > > >I want to get a folder path from a a cell and have a check function where the
    > > >function would check if the string's last character is "\"
    > > >
    > > >So that if the String is "C:\folderpath" the string would return
    > > >"C:\folderpath\" and leave it as is if the String is "C:\folderpath\"
    > > >
    > > >What VBA function would allow me to do so?

    > >
    > > Just another method:
    > >
    > > path = "C:\folderpath"
    > > Replace(path & "\", "\\", "\")
    > >
    > > --ron

    >
    > --
    >
    > Dave Peterson


    --

    Dave Peterson

  7. #7
    Ron Rosenfeld
    Guest

    Re: Checking a String's last character

    On Wed, 02 Aug 2006 10:27:54 -0500, Dave Peterson <[email protected]>
    wrote:

    >er, UNC paths....
    >
    >Darn fingers!
    >
    >Dave Peterson wrote:
    >>
    >> I'd watch out for UND paths, though:
    >>
    >> Dim myPath As String
    >> myPath = "\\share\folderpath"
    >> myPath = Replace(myPath & "\", "\\", "\")
    >>


    Excellent point! and one which I overlooked.


    --ron

+ 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