通过在互联网上搜索,我没有找到可以快速集成到我的代码中的简单解决方案。我提出我的解决方案。
实际上只需一行代码就可以做到这一点:
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