Vba 从文件路径提取文件夹名称

Vba 从文件路径提取文件夹名称,vba,Vba,目前,我正在excel vba上使用此选项生成一个文本框,用于输入文件路径: Dim FilePath As String Cells.Select Selection.ClearContents FilePath = InputBox("Hi Production Controller! Where is your file path?") 我需要从该文件路径中提取2020年2月14日: Dim FilePath As String Cells.Select Selection.ClearC

目前,我正在excel vba上使用此选项生成一个文本框,用于输入文件路径:

Dim FilePath As String
Cells.Select
Selection.ClearContents
FilePath = InputBox("Hi Production Controller! Where is your file path?")
我需要从该文件路径中提取2020年2月14日:

Dim FilePath As String
Cells.Select
Selection.ClearContents
FilePath = InputBox("Hi Production Controller! Where is your file path?")
O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020

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

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

Dim FilePath As String
Cells.Select
Selection.ClearContents
FilePath = InputBox("Hi Production Controller! Where is your file path?")
O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020

试试这个

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
选项显式
子样本()
调试。打印GetFileFolderFromPath(“O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020”)
端接头
公共函数GetFileFolderFromPath(ByVal strPath作为字符串)作为字符串
如果右$(strPath,1)“\”和Len(strPath)>0,则_
GetFileFolderFromPath=GetFileFolderFromPath(左$(strPath,Len(strPath)-1))+_
右$(strPath,1)
端函数
我需要从该文件路径中提取2020年2月14日:

Dim FilePath As String
Cells.Select
Selection.ClearContents
FilePath = InputBox("Hi Production Controller! Where is your file path?")
O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020

试试这个

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
选项显式
子样本()
调试。打印GetFileFolderFromPath(“O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020”)
端接头
公共函数GetFileFolderFromPath(ByVal strPath作为字符串)作为字符串
如果右$(strPath,1)“\”和Len(strPath)>0,则_
GetFileFolderFromPath=GetFileFolderFromPath(左$(strPath,Len(strPath)-1))+_
右$(strPath,1)
端函数

使用黑色斜杠将其拆分,然后获取数组中的最后一项:

Sub test()
  Dim sText As String
  Dim vSplit As Variant

  sText = "O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020"

  ' make sure folder string does not end with a backslash
  If Right$(sText, 1) = "\" Then sText = Left$(sText, Len(sText) - 1)

  ' Split into an array based on the backslash
  vSplit = Split(sText, "\")

  ' set cell C1 equal to the last item in the split array
  Range("C1") = vSplit(UBound(vSplit))

End Sub

使用黑色斜杠将其拆分,然后获取数组中的最后一项:

Sub test()
  Dim sText As String
  Dim vSplit As Variant

  sText = "O:\Folder1\Folder2\Folder3\2020\02 Feb\14 Feb 2020"

  ' make sure folder string does not end with a backslash
  If Right$(sText, 1) = "\" Then sText = Left$(sText, Len(sText) - 1)

  ' Split into an array based on the backslash
  vSplit = Split(sText, "\")

  ' set cell C1 equal to the last item in the split array
  Range("C1") = vSplit(UBound(vSplit))

End Sub