Checking a String's last character

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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
 
dim myPath as string
mypath = "c:\folderpath"
if right(mypath,1) <> "\" then
mypath = mypath & "\"
end if
 
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
 
I'd watch out for UND paths, though:

Dim myPath As String
myPath = "\\share\folderpath"
myPath = Replace(myPath & "\", "\\", "\")
 
er, UNC paths....

Darn fingers!

Dave said:
I'd watch out for UND paths, though:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top