2012-04-24 84 views
3

我正在使用文件系統對象遍歷一組目錄,並且我想指定一個附加目錄來循環。例如,我現在有:指定要通過Excel/VBA循環的其他目錄

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 

但是,我想通過指定附加文件夾環,如:

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

可替代地,是有可能在一個循環命令指定多個對象如:

For Each objSubfolder in colSubfolders, "additionalpath" 
+0

你可以做第二個單獨的循環。如果您希望第二個循環成爲文件夾列表,請創建一個字符串數組並遍歷該數組。 – Marc 2012-04-24 17:50:19

回答

3

簡單的方法是打出來,列舉目錄和代碼,「需要一些行動」的代碼,將其套在一個呼叫重複你所需要的;

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 
+0

感謝您的提示 - 應該考慮包括行動。 – mike 2012-04-24 18:07:46