当前,我正在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的初学者。
答案 0 :(得分:2)
我需要从此文件路径中提取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
答案 1 :(得分:0)
使用黑斜线将其拆分,然后获取数组中的最后一项:
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