简体   繁体   中英

Get folder path from file inside this folder path in Excel VBA

I was surprised that it wasn't so easy to find through searching in internet simple solution for this purpose which can be quickly integrated into my code. In many cases, answers are integrated with other things. I will propose my solution and will wait for other answers where this problem is solved.

As this function from time to time, I need in my projects I decided to create a separate function for it. The code of it is below:

Function getFolderPathFromFilePath(filePath As String) As String

    Dim lastPathSeparatorPosition As Long

    lastPathSeparatorPosition = InStrRev(filePath, Application.PathSeparator)

    getFolderPathFromFilePath = Left(filePath, lastPathSeparatorPosition - 1)

End Function

In some solutions for this purpose, I used FSO, but it takes resources, and I think it isn't worthy to create FSO object if you need it only for this simple function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM