fork download
  1. Option Explicit
  2.  
  3. Public Sub 同じ行にまとめ()
  4. Dim wb1 As Workbook 'Book1
  5. Dim wb2 As Workbook 'Book2
  6. Dim ws1 As Worksheet '売上明細
  7. Dim ws2 As Worksheet '進捗状況
  8. Dim ws3 As Worksheet 'Sheet1
  9. Dim dicT1 As Object 'キー:売上月+部門+チーム+商品コード+商品名 値:Sheet1の行番号
  10. Dim dicT2 As Object 'キー:売上月+商品名 値:Sheet1の行番号
  11. Dim maxrow1 As Long
  12. Dim maxrow2 As Long
  13. Dim row1 As Long
  14. Dim row2 As Long
  15. Dim row3 As Long
  16. Dim maxrow3 As Long: maxrow3 = 2
  17. Dim key1 As String
  18. Dim key2 As String
  19. Dim kind As String
  20. Dim kinds As Variant: kinds = Array("確認中", "出荷準備中", "出荷済")
  21. Dim i As Long
  22. Dim col3 As Long
  23. Set dicT1 = CreateObject("Scripting.Dictionary")
  24. Set dicT2 = CreateObject("Scripting.Dictionary")
  25. Set ws3 = Worksheets("Sheet1")
  26. ws3.Rows("2:" & Rows.Count).ClearContents
  27.  
  28. Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\" & "Book1.xlsx")
  29. Set ws1 = wb1.Worksheets("売上明細")
  30. Set wb2 = Workbooks.Open(ThisWorkbook.Path & "\" & "Book2.xlsx")
  31. Set ws2 = wb2.Worksheets("進捗状況")
  32. maxrow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row '最大行取得
  33. maxrow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row '最大行取得
  34. '売上明細読込
  35. For row1 = 2 To maxrow1
  36. key1 = ws1.Cells(row1, 1).Value & "|" & ws1.Cells(row1, 2).Value & "|" & ws1.Cells(row1, 3).Value & "|" & ws1.Cells(row1, 4).Value & "|" & ws1.Cells(row1, 5).Value
  37. key2 = ws1.Cells(row1, 1).Value & "|" & ws1.Cells(row1, 5).Value
  38. If dicT1.exists(key1) = False Then
  39. dicT1(key1) = maxrow3
  40. '売上月~商品名
  41. ws3.Cells(maxrow3, 1).Resize(1, 5).Value = ws1.Cells(row1, 1).Resize(1, 5).Value
  42. '合計金額
  43. ws3.Cells(maxrow3, 6).Value = ws1.Cells(row1, 6).Value
  44. '進捗状態
  45. ws3.Cells(maxrow3, 7).Value = 0
  46. ws3.Cells(maxrow3, 8).Value = 0
  47. ws3.Cells(maxrow3, 9).Value = 0
  48. If dicT2.exists(key2) = False Then
  49. dicT2(key2) = maxrow3
  50. Else
  51. MsgBox ("売上月+商品名で不正な重複発生")
  52. ws1.Activate
  53. ws1.Cells(row1, 1).Select
  54. Exit Sub
  55. End If
  56. maxrow3 = maxrow3 + 1
  57. Else
  58. '合計金額加算
  59. row3 = dicT1(key1)
  60. ws3.Cells(row3, 6).Value = ws3.Cells(row3, 6).Value + ws1.Cells(row1, 6).Value
  61. End If
  62. Next
  63. '進捗状況読込
  64. For row2 = 2 To maxrow2
  65. key2 = ws2.Cells(row2, 2).Value & "|" & ws2.Cells(row2, 3).Value
  66. If dicT2.exists(key2) = False Then
  67. MsgBox ("売上月+商品名が売上明細になし")
  68. ws2.Activate
  69. ws2.Cells(row2, 2).Select
  70. Exit Sub
  71. End If
  72. kind = ws2.Cells(row2, 1).Value
  73. i = get_index(kind, kinds)
  74. If i = -1 Then
  75. MsgBox ("進捗状況が不正")
  76. ws2.Activate
  77. ws2.Cells(row2, 1).Select
  78. Exit Sub
  79. End If
  80. '進捗状態加算
  81. row3 = dicT2(key2)
  82. col3 = 7 + i
  83. ws3.Cells(row3, col3).Value = ws3.Cells(row3, col3).Value + 1
  84. Next
  85. wb1.Close
  86. wb2.Close
  87. MsgBox ("完了")
  88. End Sub
  89. Private Function get_index(ByVal kind As String, ByVal kinds As Variant) As Long
  90. Dim i As Long
  91. For i = 0 To UBound(kinds)
  92. If kind = kinds(i) Then
  93. get_index = i
  94. Exit Function
  95. End If
  96. Next
  97. get_index = -1
  98. End Function
  99.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty