VBA搜索文件夹名称并创建文件夹路径列表

时间:2020-08-12 18:59:46

标签: vba

我正在使用此代码,但是我试图对其进行更改以获取文件夹路径列表。该代码在目录中查找在单元格C和D中输入的文本的文件夹名称,但是返回结果时没有循环性。

Sub GetPath()

    Dim searchFolderName As String
    
    searchFolderName = "D:\"
    
    Dim FileSystem As Object

    Set FileSystem = CreateObject("Scripting.FileSystemObject")

   doFolder FileSystem.GetFolder(searchFolderName)

End Sub

Sub doFolder(folder)
    Dim subFolder
    Dim myLastRow As Long, myRow As Long
    myLastRow = Cells(Rows.Count, "C").End(xlUp).Row
    
    On Error Resume Next
    For myRow = 3 To myLastRow
    If Cells(myRow, "C") = "" Then
            On Error Resume Next
              End If
              FolderName = "P_BNB_" & Cells(myRow, "C") & "_" & Cells(myRow, "D")
    For Each subFolder In folder.subfolders
        If Split(subFolder, "\")(UBound(Split(subFolder, "\"))) = FolderName Then
            Sheets("Create Folders").Cells(myRow, "E") = subFolder
             
        End
        End If

        doFolder subFolder
    Next subFolder
Next myRow
End Sub

0 个答案:

没有答案