Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-使用子文件夹中的数据循环VBA_Vba_Excel - Fatal编程技术网

Excel VBA-使用子文件夹中的数据循环VBA

Excel VBA-使用子文件夹中的数据循环VBA,vba,excel,Vba,Excel,我有一个主Excel文件和几个子文件夹中的CSV数据。现在我想从一个子文件夹加载CSV,启动另一个VBA脚本,然后转到下一个子文件夹 例如: MyExcelFile.xlsm 国家1 ../Data1.csv ../Data2.csv 国家2 ../Data3.csv ../Data4.csv 国家1报告1.csv报告2.csv国家2 Report3.csv Report4.csv 从Country1加载所有CSV,生成报告,然后转到Country2并使用此数据生成报告 以下是我加载CSV的

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

例如:

  • MyExcelFile.xlsm
  • 国家1
  • ../Data1.csv
  • ../Data2.csv
  • 国家2
  • ../Data3.csv
  • ../Data4.csv
国家1报告1.csv报告2.csv国家2 Report3.csv Report4.csv

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

以下是我加载CSV的VBA(多亏了提到的作者):

有人能给我解释一下,我怎样才能找到所有的子文件夹并将“子文件夹名称”交给ImportCVS CSV?我整个下午都在找这个,但找不到答案


提前非常感谢:-)

这里的概念是创建对象。我的方法是循环浏览目标文件夹(包括其子文件夹)中的所有CSV文件,然后将符合我的条件的CSV导入新的临时文件夹。 然后,您可以使用当前代码将所有CSV加载到mastersheet,重命名并控制临时文件夹。希望这有帮助

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

非常感谢你的帮助。我通过以下代码实现了我想要的功能:

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的回答标记为答案,而不是你自己的答案。当然,改变了它:-)
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