A question I get asked a lot is how to extract a filename from a full filepath.
Whilst you can use InStr and loop through the filepath I have found it much easier to start at the other end of the path and work backwards
i.e. Passing in a FullPath of "C:\MyDocuments\Test.txt" to the below function will extract a filename of Test.txt
Code:
Function FileName (FullPath as string)
Dim FileName as string
Dim Pos as Integer
Pos = InStrRev (FullPath,"\",,vbTextCompare)
FileName = Mid(FullPath,Pos +1)
Debug.Print FileName
Exit Function
The code starts at the end of the string and Pos in the position of the first "\" moving backwards.
Using the Mid function you can then extract the filename by starting at position pos+1
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.