首页 > 解决方案 > 从文件路径中提取文件夹名称

问题描述

目前,我在 excel vba 上使用它来生成一个文本框供我输入文件路径:

Dim FilePath As String
Cells.Select
Selection.ClearContents
FilePath = InputBox("Hi Production Controller! Where is your file path?")

我需要从此文件路径中提取 2020 年 2 月 14 日:

O:\Folder1\Folder2\Folder3\2020\02 二月\2020 年 2 月 14 日

并将其插入单元格 C1。我能得到一些帮助吗?我是vba的初学者。

标签: vba

解决方案


我需要从此文件路径中提取 2020 年 2 月 14 日:

O:\Folder1\Folder2\Folder3\2020\02 二月\2020 年 2 月 14 日

尝试这个

Option Explicit

Sub Sample()
    Debug.Print GetFileFolderFromPath("O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020")
End Sub

Public Function GetFileFolderFromPath(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then _
    GetFileFolderFromPath = GetFileFolderFromPath(Left$(strPath, Len(strPath) - 1)) + _
    Right$(strPath, 1)
End Function

推荐阅读