从此文件夹路径中的文件获取文件夹路径 Excel VBA

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

令我惊讶的是,通过在互联网上搜索可以快速集成到我的代码中的简单解决方案并不是那么容易找到的。在许多情况下,答案与其他事物结合在一起。我将提出我的解决方案,并等待解决此问题的其他答案。

由于这个功能时常出现,我需要在我的项目中为它创建一个单独的功能。它的代码如下:

Function getFolderPathFromFilePath(filePath As String) As String

    Dim lastPathSeparatorPosition As Long

    lastPathSeparatorPosition = InStrRev(filePath, Application.PathSeparator)

    getFolderPathFromFilePath = Left(filePath, lastPathSeparatorPosition - 1)

End Function

在为此目的的一些解决方案中,我使用了 FSO,但是它占用资源,而且我认为如果您只需要为这个简单的功能创建 FSO 对象是不值得的。