2017-07-27 38 views
2

我想要訪問我當前文件夾的每個子文件夾(每個子文件夾中的子文件夾數可能不同),然後希望在每個Excel工作簿中執行一些操作所有這些子文件夾。問題在循環瀏覽文件夾的各種子文件夾和這些子文件夾的每個文件

下面提到的是代碼和代碼不會拋出編譯時錯誤,但不工作。請幫助我

option explicit 
Sub LoopFolders() 
    Dim strFolder As String 
    Dim strSubFolder As String 
    Dim strFile As String 
    Dim colSubFolders As New Collection 
    Dim varItem As Variant 
    Dim wbk As Workbook 
    ' Parent folder including trailing backslash 
    strFolder = "C:\Users\Yashika Vaish\Desktop\yashika\" 
    ' Loop through the subfolders and fill Collection object 
    strSubFolder = Dir(strFolder & "*", vbDirectory) 
    Do While Not strSubFolder = "" 
     Select Case strSubFolder 
      Case ".", ".." 
       ' Current folder or parent folder - ignore 
      Case Else 
       ' Add to collection 
       colSubFolders.Add Item:=strSubFolder, Key:=strSubFolder 
     End Select 
     ' On to the next one 
     strSubFolder = Dir 
    Loop 
    ' Loop through the collection 
    For Each varItem In colSubFolders 
     ' Loop through Excel workbooks in subfolder 
     strFile = Dir(strFolder & varItem & "\*.xls*") 
     Do While strFile <> "" 
      ' Open workbook 
      Set wbk = Workbooks.Open(FileName:=strFolder & _ 
       varItem & "\" & strFile, AddToMRU:=False) 
      MsgBox "I am open" 

      strFile = Dir 
     Loop 
    Next varItem 
End Sub 

工具設置中的所有必需參考都已添加到此VBA項目中。請幫助我使用此代碼。

+0

你究竟想要處理子文件夾中的文件? – ThatOneGuy

+0

我需要打開這些文件並導入一些宏並運行這些文件,但在訪問所有子文件夾時會出現問題。請幫助我解決這個問題 –

+0

「不工作」對您沒有幫助。代碼與預期的結果是什麼?你應該拋出一些'Debug.Print'調用來找出正在被發現的目錄和應該找到的目錄。上面的代碼沒有明顯的理由不起作用。我假設你只會深入一層?上面的代碼對於任意深度不是遞歸的。通常,搜索文件/文件夾的問題歸結爲缺少路徑分隔符或在驗證代碼後搜索錯誤的目錄。 –

回答

2

以下方法將子文件夾中的文件名寫入工作簿。所以它找到了它們。

Sub Program() 
    Dim i As Integer 
    i = 1 
    listFiles "D:\Folder 1", i 
End Sub 

Sub listFiles(ByVal sPath As String, ByRef i As Integer) 
    Dim vaArray  As Variant 
    Dim oFile  As Object 
    Dim oFSO  As Object 
    Dim oFolder  As Object 
    Dim oFiles  As Object 

    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    Set oFolder = oFSO.GetFolder(sPath) 
    Set oFiles = oFolder.Files 

    If (oFiles.Count > 0) Then 
     ReDim vaArray(1 To oFiles.Count) 
     For Each oFile In oFiles 
      Cells(i, "A").Value = oFile.Name 
      Cells(i, "B").Value = oFile.Path 
      i = i + 1 
     Next 
    End If 

    listFolders sPath, i 
End Sub 

Sub listFolders(ByVal sPath As String, ByRef i As Integer) 
    Dim vaArray  As Variant 
    Dim oFile  As Object 
    Dim oFSO  As Object 
    Dim oFolder  As Object 
    Dim oFiles  As Object 

    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    Set oFolder = oFSO.GetFolder(sPath) 
    Set oFiles = oFolder.subfolders 

    If (oFiles.Count > 0) Then 
     ReDim vaArray(1 To oFiles.Count) 
     For Each oFile In oFiles 
      listFiles oFile.Path, i 
      i = i + 1 
     Next 
    End If 
End Sub 
+0

非常感謝你,但是這段代碼不工作,並給我運行時錯誤28「超出範圍下標」我的目的是通過文件夾的所有子文件夾,請幫助我。 –

0

所以,這裏是我如何通過搜索文件夾來查找特定的文件類型。 (早期綁定是你在開發中的朋友)。確保您已啓用Microsoft腳本運行時參考。

Option Explicit 
Sub test() 
Dim fso As Scripting.FileSystemObject 
Dim fsoMainDirectory As Scripting.Folder 
Dim fsoSubfolder As Scripting.Folder 
Dim fsoFile As Scripting.File 
Dim strFilePath 

Set fso = New Scripting.FileSystemObject 
Set fsoMainDirectory = fso.GetFolder("Directory, with trailing \") 

For Each fsoFile In fsoMainDirectory.Files 
    If fsoFile.Type = "Microsoft Excel 97-2003 Worksheet" Then '.xls file type 
     strFilePath = fsoFile.Path 
     Application.Workbooks.Open strFilePath 
    End If 
Next fsoFile 
End Sub 

你的子文件夾有多深?你是唯一一個會使用這個宏嗎?使用未知數量的子文件夾循環訪問n個子文件夾是可行的,但我的方法涉及一系列計數器。這個數組可能會降低性能,因此如果我們不需要這樣做就不想這樣做。

+0

感謝您的分享,但此代碼並未循環通過任何子文件夾,我的目的是通過我父文件夾的每個子文件夾和每個y子文件夾的工作簿。請幫助我 –

相關問題