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

十年热恋 提交于 2020-03-05 01:32:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/58232772/get-folder-path-from-file-inside-this-folder-path-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!