Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Extract Path From String
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Extract Path From String
It needs to be compatable with at least XL2000.
"Dave Peterson" wrote in message ... 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Relative Path in Connection String | Excel Discussion (Misc queries) | |||
How to extract only file name from folder path string in Excel? | Excel Worksheet Functions | |||
Extract full path given filename | Excel Programming | |||
chdir not accepting string for path | Excel Programming | |||
Getting a String Path from a browser... | Excel Programming |