プログラミング

フォルダ内のエクセルファイルを一つのブックにまとめるVBA

Sub CombineWorkbooks()
    Dim folderPath As String
    Dim fileName As String
    Dim sourceWorkbook As Workbook
    Dim targetWorkbook As Workbook
    
    folderPath = "C:\your\folder\path\"
    fileName = "CombinedWorkbook.xlsx"
    
    Set targetWorkbook = ThisWorkbook
    targetWorkbook.Sheets.Add after:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
    
    fileName = Dir(folderPath & "*.xlsx")
    Do While fileName <> ""
        Set sourceWorkbook = Workbooks.Open(folderPath & fileName)
        sourceWorkbook.Sheets(1).Copy after:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
        sourceWorkbook.Close
        fileName = Dir()
    Loop
End Sub

-プログラミング