繁体   English   中英

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

[英]Get folder name from path in Excel VBA

为此,我没有通过在互联网上搜索的简单解决方案找到可以快速集成到我的代码中的解决方案。 我提出我的解决方案。

你实际上可以用一行代码来做到这一点:

Function GetFolderNameFromPath(folderPath As String) As String
    GetFolderNameFromPath = Split(folderPath, Application.PathSeparator)(UBound(Split(folderPath, Application.PathSeparator)))
End Function

由于此功能不时,我需要在我的项目中决定为它创建一个单独的功能。 它的代码如下:

Function GetFolderNameFromPath(folderPath As String) As String

Dim lastPathSeparatorPosition As Long, folderPathLength As Long, folderNameLength As Long

lastPathSeparatorPosition = InStrRev(folderPath, Application.PathSeparator)
folderPathLength = Len(folderPath)
folderNameLength = folderPathLength - lastPathSeparatorPosition
GetFolderNameFromPath = Right(folderPath, folderNameLength)

End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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