0

いくつかのサブフォルダーがあります。それぞれにテキストファイルがあります。Excel タブごとに 1 つのファイルが存在するように、テキスト ファイルを 1 つの Excel ファイルにグループ化することができます。このタスクを実行するコードを設計しました。

Option Explicit
Sub read_files()
Dim ReadData As String
Dim i As Double
Dim objfso As Object
Dim objfolder As Object
Dim obj_sub_folder As Object
Dim objfile As Object
Dim current_worksheet As Worksheet
Dim new_workbook As Workbook
Dim path As String
Dim filestream As Integer


Set objfso = CreateObject("Scripting.FilesystemObject")
Set objfolder = objfso.getfolder("Z:\test\")
Set new_workbook = Workbooks.Add
i = 1

For Each obj_sub_folder In objfolder.subfolders
    i = 1
    ReadData = ""
    For Each objfile In obj_sub_folder.Files
        Set current_worksheet = new_workbook.Worksheets.Add
        current_worksheet.Name = objfile.Name
        filestream = FreeFile()
        path = "Z:\test\" & obj_sub_folder.Name & "\" & objfile.Name
        Open path For Input As #filestream
        Do Until EOF(filestream)
            Input #filestream, ReadData
            current_worksheet.Cells(i, 1).Value = ReadData
            i = i + 1
        Loop
        Close filestream
    Next
    ActiveWorkbook.SaveAs "Z:\test\" & obj_sub_folder.Name
Next End Sub

ただし、サブフォルダーをループしている間、マクロは前のサブフォルダーのファイルからデータを保存しますが、特定のサブフォルダーからのファイルからデータを保存したいと思います。どこが間違っているのか説明していただけませんか?

ありがとうございました!

編集

ここに作業コードがあります

Option Explicit
Sub run()
     read_files ("Z:\test\")
End Sub
Sub read_files(path_to_folder As String)
Dim ReadData As String
Dim i As Double
Dim objfso As Object
Dim objfolder As Object
Dim obj_sub_folder As Object
Dim objfile As Object
Dim current_worksheet As Worksheet
Dim new_workbook As Workbook
Dim path As String
Dim filestream As Integer

Set objfso = CreateObject("Scripting.FilesystemObject")
Set objfolder = objfso.getfolder(path_to_folder)
i = 1

For Each obj_sub_folder In objfolder.subfolders
    Set new_workbook = Workbooks.Add

    For Each objfile In obj_sub_folder.Files
        Set current_worksheet = new_workbook.Worksheets.Add
        current_worksheet.Name = objfile.Name
        filestream = FreeFile()
        path = path_to_folder & obj_sub_folder.Name & "\" & objfile.Name
        Open path For Input As #filestream
        Do Until EOF(filestream)
            Input #filestream, ReadData
            current_worksheet.Cells(i, 1).Value = ReadData
            i = i + 1
        Loop
        Close filestream
        i = 1
    Next
    ActiveWorkbook.SaveAs path & obj_sub_folder.Name
    ActiveWorkbook.Close
Next

サブ終了

4

1 に答える 1

2

各サブフォルダーのデータを個別のワークブックに配置する場合は、new_workbook定義をFor Each obj_sub_folderループ内に移動し、保存後にそのワークブックを閉じる 必要があります。

Set objfso = CreateObject("Scripting.FilesystemObject")
Set objfolder = objfso.getfolder("Z:\test\")
i = 1

For Each obj_sub_folder In objfolder.subfolders
    Set new_workbook = Workbooks.Add
    i = 1
    ReadData = ""
    For Each objfile In obj_sub_folder.Files
        Set current_worksheet = new_workbook.Worksheets.Add
        current_worksheet.Name = objfile.Name
        filestream = FreeFile()
        path = "Z:\test\" & obj_sub_folder.Name & "\" & objfile.Name
        Open path For Input As #filestream
        Do Until EOF(filestream)
            Input #filestream, ReadData
            current_worksheet.Cells(i, 1).Value = ReadData
            i = i + 1
        Loop
        Close filestream
    Next
    new_workbook.SaveAs "Z:\test\" & obj_sub_folder.Name
    new_workbook.Close
Next 
于 2013-09-09T20:36:19.533 に答える