问题
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