如何使用Excel从文件名获取文件路径?

时间:2019-05-16 19:59:20

标签: excel vba

我在excel中有一个文件名列表。我想遍历每个名​​称,并让它们的路径显示在它们旁边的栏中。所有这些文件都位于一个基本文件夹中,该文件夹中有多个文件夹。因此,我希望这样的内容出现在文件名行的旁边:

C:\ Users \ RUMI \ BaseFolder \ FolderA \

这是我尝试遍历不属于我的文件夹的一些代码。

Sub TraversePath(path As String)
Dim currentPath As String, directory As Variant
Dim dirCollection As Collection
Set dirCollection = New Collection

currentPath = Dir(path, vbDirectory)

'Explore current directory
Do Until currentPath = vbNullString
    Debug.Print currentPath
    If Left(currentPath, 1) <> "." And _
        (GetAttr(path & currentPath) And vbDirectory) = vbDirectory Then
        dirCollection.Add currentPath
    End If
    currentPath = Dir()
Loop

'Explore subsequent directories
For Each directory In dirCollection
    Debug.Print "---SubDirectory: " & directory & "---"
    TraversePath path & directory & "\"
Next directory
End Sub


Sub Test()
    TraversePath "C:\Users\RUMI\"
End Sub

0 个答案:

没有答案