Skip to content

Instantly share code, notes, and snippets.

@tsunakan
Created July 4, 2016 00:11
Show Gist options
  • Save tsunakan/e59dc1f373c224c53f6eeebb21703b66 to your computer and use it in GitHub Desktop.
Save tsunakan/e59dc1f373c224c53f6eeebb21703b66 to your computer and use it in GitHub Desktop.
フォルダ内のすべてのCSVファイルを、1つのbook上にsheetに分けて読み込む
Option Explicit
Sub CSVAggregation()
Dim Folder_Path As String
Dim File_Path As String
Dim Aggregation_Book As Workbook
Dim Copy_Sheet As Worksheet
'フォルダを取得
ChDir ThisWorkbook.Path & "\"
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then
Exit Sub
End If
Folder_Path = .SelectedItems(1)
End With
'新しいBookを作成(ここにまとめていく)
Application.SheetsInNewWorkbook = 1
Workbooks.Add
Set Aggregation_Book = ActiveWorkbook
'フォルダ内のすべてのCSVファイルをBookにコピーする
File_Path = Dir(Folder_Path & "\*.csv")
Do Until File_Path = ""
Workbooks.Open Filename:=Folder_Path & "\" & File_Path
Set Copy_Sheet = ActiveWorkbook.Worksheets(1)
Copy_Sheet.Copy After:=Aggregation_Book.Worksheets _
(Aggregation_Book.Worksheets.Count)
Workbooks(File_Path).Close
'↓読み込むSheetに処理をしたかったら(列幅の調整やウィンドウ枠の固定)↓
''''''''''''ここから''''''''''''
Cells.Font.Size = 10
Columns("A:AB").AutoFit
''''''''''''ここまで''''''''''''
File_Path = Dir()
Loop
'新しいBookを作った時にできる”Sheet1”を削除
Application.DisplayAlerts = False
Worksheets(1).Delete
Application.DisplayAlerts = True
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment