如何使程序循环遍历所选文件夹的所有子文件夹?

时间:2019-07-15 19:33:38

标签: excel vba

因此,我开发了Excel中的宏(阅读:google->复制/粘贴),用于将来自众多标准化excel模板的数据编译到一个主文件中。目前,该程序可以循环访问单个文件夹中的任意数量的文件,但是我希望能够从该文件夹中选择一个级别,并在需要时使其遍历所有子文件夹,但是在单个文件夹中仍然可以工作被选中。

打开文件后启动的代码全部在打开的文件上完成。我还有其他几行,但是所有这些行都在编译文件上运行,因此它不需要包含在文件循环部分中。

我尝试使用类似问题中给出的一些解决方案,但是它从来没有为我正确运行过。使用会导致缺少结束(如果/有错误)。

'Selects template folder location for macro
Sub LoopThroughFiles()
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)

'Transfers data from template to master file
    Worksheets("International").Activate
    Range("A4:L4").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("Architecture Master Macro_Test.xlsm").Activate
    '"Workbooks.Open "C:\ArchNonMacro.xlsx"
    'Windows("ArchNonMacro.xlsx").Activate

    Range("A1").Select
    Selection.End(xlDown).Select
    Selection.Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Application.DisplayAlerts = False
    Workbooks(xFileName).Close

End Sub

0 个答案:

没有答案
相关问题