【问题标题】:Excel VBA - Loop VBAs with Data from SubfolderExcel VBA - 使用子文件夹中的数据循环 VBA
【发布时间】:2017-11-27 17:38:42
【问题描述】:

我在几个子文件夹中有一个主 Excel 文件和 CSV 数据。我现在想从一个子文件夹加载 CSV,启动另一个 VBA 脚本,然后转到下一个子文件夹。

例子:

  • MyExcelFile.xlsm
  • 国家 1
  • ../Data1.csv
  • ../Data2.csv
  • 国家 2
  • ../Data3.csv
  • ../Data4.csv

Country1 Report1.csv Report2.csv Country2 报告3.csv 报告4.csv

从 Country1 加载所有 CSV,生成报告,然后转到 Country2 并使用此数据生成报告。

这是我用来加载 CSV 的 VBA(感谢提到的作者):

Sub ImportCSVs()
'Author:    Jerry Beaucaire
'Date:      8/16/2010
'Summary:   Import all CSV files from a folder into separate sheets
Dim fPath   As String
Dim fCSV    As String
Dim wbCSV   As Workbook
Dim wbMST   As Workbook

Set wbMST = ThisWorkbook
fPath = (Application.ActiveWorkbook.Path & "\")                  'path to     CSV files, include the final \
Application.ScreenUpdating = False  'speed up macro
Application.DisplayAlerts = False   'no error messages, take default answers
fCSV = Dir(fPath & "*.txt")         'start the CSV file listing

    On Error Resume Next
    Do While Len(fCSV) > 0
        Set wbCSV = Workbooks.Open(fPath & fCSV, xlDelimited, Delimiter:=",", Format:=6, Local:=False)                  'open a CSV file
        wbMST.Sheets(ActiveSheet.Name).Delete                       'delete sheet if it exists
        ActiveSheet.Move After:=wbMST.Sheets(wbMST.Sheets.Count)    'move new sheet into Mstr
        Columns.AutoFit             'clean up display
        fCSV = Dir                  'ready next CSV
    Loop

Application.ScreenUpdating = True
Set wbCSV = Nothing
End Sub

谁能解释一下,我怎样才能转到所有子文件夹并移交 ImportCSVs-CSV 的“子文件夹名称”?我整个下午都在寻找这个,但找不到答案。

提前非常感谢你:-)

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    非常感谢您的帮助。我设法用下面的代码做我想做的事:

    Sub RunAll()
    
    Dim Fso As Object, objFolder As Object, objSubFolder As Object, tempFolder 
    As Object
    Dim FromPath As String
    Dim fpath As String
    Dim FileInFolder As Object
    Dim ToPath As String
    Dim temporaryFolder As String
    
    temporaryFolder = "Temp"
    fpath = (Application.ActiveWorkbook.Path & "\")
    FromPath = fpath
    ToPath = fpath & temporaryFolder & "\"
    Set Fso = CreateObject("Scripting.filesystemobject")
    
    Set objFolder = Fso.GetFolder(FromPath)
    
    'clean Masterfolder first
    Set tempFolder = Fso.GetFolder(ToPath)
    
    'loop through each subfolders
    For Each objSubFolder In objFolder.subfolders
        For Each File In tempFolder.Files
            File.Delete
        Next File
    
        For Each FileInFolder In objSubFolder.Files
            If FileInFolder.Name Like "*REPORT*.txt" Then 'criteria
                FileInFolder.Copy ToPath
            End If
        Next FileInFolder
    
        'Check if folder is empty
        If Dir(ToPath & "*.*") = "" Then
    
        Else
            Call ImportCSVs
            Call ImportData
            Call PrintPDF
        End If
    
    
    Next objSubFolder
    
    Call CloseFile
    
    End Sub
    

    【讨论】:

    • 公平起见,我认为您应该将@NoAppleOnHead 的回复标记为答案,而不是您自己的答案。
    • 当然,改了:-)
    【解决方案2】:

    创建对象是这里的概念。我的方法是遍历目标文件夹(包括其子文件夹)中的所有 CSV 文件,然后将符合我条件的那些 CSV 导入到一个新的临时文件夹中。 然后,您可以使用当前代码将所有 CSV 加载到母版表,重命名和控制临时文件夹。希望这会有所帮助。

    Dim Fso As Object, objFolder As Object, objSubFolder As Object, tempFolder As Object
    Dim FromPath As String
    Dim FileInFolder As Object
    Dim ToPath As String
    
    ToPath = "V:\MasterFolder\"
    FromPath = "V:\TargetFolder\"
    Set Fso = CreateObject("Scripting.filesystemobject")
    
    'clean Masterfolder first
    Set tempFolder = Fso.GetFolder(ToPath)
    For Each File In tempFolder.Files
        File.Delete
    Next File
    
    'loop through each subfolders
    For Each objSubFolder In objFolder.subfolders
        For Each FileInFolder In objSubFolder.Files
            If FileInFolder.Name Like "*DATA*" Then 'criteria
                FileInFolder.Copy ToPath
            End If
        Next FileInFolder
    Next objSubFolder
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 2014-05-03
      相关资源
      最近更新 更多