[英]Extracting folder name from file path
目前,我在 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 Feb\\14 Feb 2020
并将其插入单元格 C1。 我能得到一些帮助吗? 我是 vba 的初学者。
我需要从此文件路径中提取 2020 年 2 月 14 日:
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
使用黑斜线拆分它,然后取数组中的最后一项:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.