View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_5_] Dave Peterson[_5_] is offline
external usenet poster
 
Posts: 1,758
Default Extract Path From String

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

Andibevan wrote:

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


--

Dave Peterson