首页 > 解决方案 > 从 Excel VBA 中此文件夹路径内的文件中获取文件夹路径

问题描述

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

标签: excelvbapathfilepath

解决方案


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

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 对象是不值得的。


推荐阅读