View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default How to split filename from filepath?

Something like this will do it:

Function FileFromPath(ByVal strFullPath As String, _
Optional bExtensionOff As Boolean = False) As String

Dim FPL As Long 'len of full path
Dim PLS As Long 'position of last slash
Dim pd As Long 'position of dot before exension
Dim strFile As String

On Error GoTo ERROROUT

FPL = Len(strFullPath)
PLS = InStrRev(strFullPath, "\", , vbBinaryCompare)
strFile = Right$(strFullPath, FPL - PLS)

If bExtensionOff = False Then
FileFromPath = strFile
Else
pd = InStr(1, strFile, ".", vbBinaryCompare)
FileFromPath = Left$(strFile, pd - 1)
End If

Exit Function
ERROROUT:

On Error GoTo 0
FileFromPath = ""

End Function


RBS


"Frank" wrote in message
...
If I retrieve a filename including the filepath like this:

strFileName = Application.GetOpenFilename("Report (*.txt; *.dat ),*.txt;
*.dat")

How do I manipulate the strFileName, retrieving only the filename?

(guess I have to search from right to find the first "\". Then delete the
content to the left of this character. But I don't know how this works in
VBA)

Thanks for suggestions.

Frank