Extract Path From String

A

Andibevan

Hi All,

If I have the following string assigned to var_full "C:\my documents\test
folder\test doc.doc"

What is the best way to extract the path from the above string so that the
output is "C:\my documents\test folder"

The output string needs to NOT finish in a backslash.

The solution also needs to be able to cope with any number of folder
hierarchies.

Any ideas?

Ta

Andi
 
D

Dave Peterson

What version of excel are you using?

xl2k+

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath as string

myStr = "C:\my documents\test folder\test doc.doc"

myPath = Left(myStr, InStrRev(myStr, "\") - 1)

MsgBox myPath

End Sub

before xl2k:

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath As String
Dim iCtr As Long

myStr = "C:\my documents\test folder\test doc.doc"

For iCtr = Len(myStr) To 1 Step -1
If Mid(myStr, iCtr, 1) = "\" Then
myPath = Left(myStr, iCtr - 1)
Exit For
End If
Next iCtr

MsgBox myPath
End Sub
 
A

Andibevan

It needs to be compatable with at least XL2000.

Dave Peterson said:
What version of excel are you using?

xl2k+

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath as string

myStr = "C:\my documents\test folder\test doc.doc"

myPath = Left(myStr, InStrRev(myStr, "\") - 1)

MsgBox myPath

End Sub

before xl2k:

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath As String
Dim iCtr As Long

myStr = "C:\my documents\test folder\test doc.doc"

For iCtr = Len(myStr) To 1 Step -1
If Mid(myStr, iCtr, 1) = "\" Then
myPath = Left(myStr, iCtr - 1)
Exit For
End If
Next iCtr

MsgBox myPath
End Sub
 

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

Top