fork download
  1. Option Explicit
  2. Public Sub 複数ブック統合()
  3. Dim twb As Workbook '新規ブック
  4. Dim sheet_names As Variant 'シート名一覧
  5. Dim sh_name As String 'シート名
  6. Dim i As Long
  7. Dim folder As String '参照先フォルダ名
  8. Dim outfolder As String '出力先フォルダ名
  9. Dim count As Long 'ファイル件数
  10. Dim bookname As String 'マージ元ブック名
  11. Dim new_bookname As String '統合ブック名
  12. Dim ks As Worksheet '管理シート
  13. Set ks = Worksheets("管理")
  14. folder = ks.Range("B5").Value
  15. outfolder = ks.Range("B9").Value
  16. If folder = "" Then
  17. MsgBox ("参照先フォルダ名未設定")
  18. Exit Sub
  19. End If
  20. If outfolder = "" Then
  21. MsgBox ("出力先フォルダ名未設定")
  22. Exit Sub
  23. End If
  24. bookname = Dir(folder & "\*.xlsx")
  25. If bookname = "" Then
  26. MsgBox (folder & "内にブックが存在しません。")
  27. Exit Sub
  28. End If
  29. Application.ScreenUpdating = False
  30. Application.Calculation = xlCalculationManual
  31.  
  32. '新規ブック及びシートの作成
  33. 'sheet_names = Array("Sheet1", "Sheet2", "シート1", "シート2")
  34. sheet_names = Array("Sheet1", "元データ", "元データ2")
  35. Set twb = Workbooks.Add
  36. '全てのシートを作成する
  37. For i = 0 To UBound(sheet_names)
  38. Call add_sheet(twb, sheet_names(i))
  39. Next
  40. '余分なシートを削除する
  41. For i = twb.Worksheets.count To 1 Step -1
  42. sh_name = twb.Worksheets(i).Name
  43. Call del_sheet(twb, sh_name, sheet_names)
  44. Next
  45. '全てのブックを処理する
  46. count = 0
  47. new_bookname = "マージ" & Left(bookname, Len(bookname) - 8) & ".xlsx"
  48. Do While bookname <> ""
  49. count = count + 1
  50. Call MergeBook(twb, sheet_names, count, folder, bookname)
  51. bookname = Dir()
  52. Loop
  53. Application.Calculation = xlCalculationAutomatic
  54. Application.ScreenUpdating = True
  55. Application.DisplayAlerts = False
  56. twb.SaveAs (outfolder & "\" & new_bookname)
  57. Application.DisplayAlerts = True
  58. twb.Close
  59. MsgBox ("完了")
  60. End Sub
  61. '新規ブックへ1シートを追加する
  62. Private Sub add_sheet(ByVal twb As Workbook, ByVal sheet_name As String)
  63. Dim i As Long
  64. For i = 1 To twb.Worksheets.count
  65. If LCase(sheet_name) = LCase(twb.Worksheets(i).Name) Then
  66. twb.Worksheets(i).Move after:=twb.Worksheets(twb.Worksheets.count)
  67. Exit Sub
  68. End If
  69. Next
  70. twb.Worksheets.Add after:=twb.Worksheets(twb.Worksheets.count)
  71. twb.Worksheets(twb.Worksheets.count).Name = sheet_name
  72. End Sub
  73. '新規ブックから余分な1シートを削除する
  74. Private Sub del_sheet(ByVal twb As Workbook, ByVal sheet_name As String, ByVal sheet_names As Variant)
  75. Dim i As Long
  76. For i = 0 To UBound(sheet_names)
  77. If LCase(sheet_name) = LCase(sheet_names(i)) Then
  78. Exit Sub
  79. End If
  80. Next
  81. Application.DisplayAlerts = False
  82. twb.Worksheets(sheet_name).Delete
  83. Application.DisplayAlerts = True
  84. End Sub
  85. '新規ブック1ブックをマージする
  86. Public Sub MergeBook(ByVal twb As Workbook, ByVal sheet_names As Variant, ByVal count As Long, ByVal folder As String, ByVal bookname As String)
  87. Dim wb As Workbook
  88. Dim sh_name As String
  89. Dim maxrow_src As Long
  90. Dim maxcol_src As Long
  91. Dim maxrow_trg As Long
  92. Dim ecol As String
  93. Dim i As Long
  94. Set wb = Workbooks.Open(folder & "\" & bookname)
  95. For i = 0 To UBound(sheet_names)
  96. sh_name = sheet_names(i)
  97. maxrow_src = wb.Worksheets(sh_name).Cells(Rows.count, "A").End(xlUp).Row 'A列 最終行を求める
  98. maxcol_src = wb.Worksheets(sh_name).Cells(1, Columns.count).End(xlToLeft).Column '1行目の最終列を求める
  99. maxrow_trg = twb.Worksheets(sh_name).Cells(Rows.count, "A").End(xlUp).Row 'A列 最終行を求める
  100. ecol = ConvertToLetter(maxcol_src)
  101. If count = 1 Then
  102. wb.Worksheets(sh_name).Range("A1:" & ecol & "3").Copy twb.Worksheets(sh_name).Range("A1")
  103. maxrow_trg = 3
  104. End If
  105. If maxrow_src > 3 Then
  106. wb.Worksheets(sh_name).Range("A4:" & ecol & maxrow_src).Copy twb.Worksheets(sh_name).Range("A" & maxrow_trg + 1)
  107. End If
  108. Next
  109. wb.Close
  110. End Sub
  111. '列番号をA~Zの文字に変換
  112. Function ConvertToLetter(iCol As Long) As String
  113. Dim a As Long
  114. Dim b As Long
  115. a = iCol
  116. ConvertToLetter = ""
  117. Do While iCol > 0
  118. a = Int((iCol - 1) / 26)
  119. b = (iCol - 1) Mod 26
  120. ConvertToLetter = Chr(b + 65) & ConvertToLetter
  121. iCol = a
  122. Loop
  123. End Function
  124.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty