Specify an additional directory for cyclic transition through Excel / VBA

I iterate over a set of directories using a file system object, and I want to specify an additional directory to scroll through. For example, I currently have:

Sub test()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Directory)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder In colSubfolders
        ' take some action
    End For
End Sub

But I want to specify an additional folder for scrolling, for example:

colSubfolders = colSubfolders + "additionalpath"
For Each objSubfolder In colSubfolders ....

Alternatively, you can specify several objects in the loop command, for example:

For Each objSubfolder in colSubfolders, "additionalpath"
+3
source share
1 answer

The easiest way is to output code that lists directories and code that "takes some action" and wrap it in a call that repeats what you need;

Sub foo()
    enumDirs "c:\temp\", "c:\music", "c:\as many as you like ..."
End Sub

Sub enumDirs(ParamArray strPaths() As Variant)
    Dim i As Long
    For i = 0 To UBound(strPaths)
        enumDir CStr(strPaths(i))
    Next
End Sub

Sub enumDir(strPath As String)
    Dim objFolder As Object, colSubfolders As Object, objSubfolder As Object
    Set objFolder = CreateObject("Scripting.FileSystemObject").GetFolder(strPath)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder In colSubfolders
        TakeSomeAction strPath, objSubfolder.Name
    Next
End Sub

sub TakeSomeAction(strRoot As String, strFoundPath As String)
    Debug.Print ">"; strRoot & ", " & strFoundPath
End sub
+3
source

All Articles