fork download
  1. Option Explicit
  2.  
  3. Const RESTCOL As String = "E" 'Aシート作業列
  4. Dim ws1 As Worksheet 'Aシート
  5. Dim ws2 As Worksheet 'Bシート
  6. Dim dicST As Object '連想配列 キー:車種 値:最初に出現する行番号(Aシート)
  7. Dim dicEN As Object '連想配列 キー:車種 値:最後に出現する行番号(Aシート)
  8. Dim Arr As Variant 'Bシートイメージ保存
  9. Dim row2 As Long 'Bシート 行番号
  10. Public Sub 転記()
  11. Dim maxrow1 As Long '最大行数 Aシート
  12. Dim maxrow2 As Long '最大行数 Bシート
  13. Dim row1 As Long '行番号 Aシート
  14. Dim ix As Long 'Arr 添え字
  15. Dim key As String '車種
  16. Set ws1 = Sheets("A")
  17. Set ws2 = Sheets("B")
  18. Set dicST = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  19. Set dicEN = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  20. maxrow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row 'A列の最大行取得
  21. maxrow2 = ws2.Cells(Rows.Count, 4).End(xlUp).Row 'D列の最大行取得
  22. '最大行数が要件を満たしてないなら処理しない
  23. If maxrow1 < 5 Then Exit Sub
  24. If maxrow2 < 5 Then Exit Sub
  25. 'Bシートを配列へ転送
  26. Arr = ws2.Range("A5:Z" & maxrow2).Value
  27. 'Bシート 5行以降をクリア
  28. ws2.Rows("5:" & Rows.Count).ClearContents
  29. row2 = 5
  30. '車種を取り込む
  31. For row1 = 5 To maxrow1
  32. key = ws1.Cells(row1, "A").Value
  33. ws1.Cells(row1, RESTCOL).Value = ws1.Cells(row1, "D").Value '作業用在庫
  34. If dicST.exists(key) = False Then
  35. dicST(key) = row1
  36. dicEN(key) = row1
  37. Else
  38. If dicEN(key) + 1 <> row1 Then
  39. ws1.Activate
  40. ws1.Cells(row1, "A").Select
  41. MsgBox ("車種が連続していません")
  42. Exit Sub
  43. End If
  44. dicEN(key) = row1
  45. End If
  46. Next
  47. 'Arrを1列毎に処理する。
  48. For ix = 1 To UBound(Arr, 1)
  49. '転記処理(Bシートの行番号)
  50. Call tenki(ix)
  51. Next
  52. ws1.Range(RESTCOL & "5:" & RESTCOL & maxrow1).ClearContents '作業列クリア
  53. MsgBox ("完了")
  54. End Sub
  55.  
  56. '転記処理 ix:Arrの添え字
  57. Private Sub tenki(ByVal ix As Long)
  58. Dim rcnt As Long '該当者車種の行数(Aシート)
  59. Dim srow1 As Long '該当車種の開始行(Aシート)
  60. Dim key As String '車種
  61. Dim rest As Variant '受注残
  62. Dim row1 As Long '行番号 Aシート
  63. Dim temp As Variant
  64. 'Bシートの1行分(A~Z)をArrから転送
  65. temp = WorksheetFunction.Index(Arr, ix)
  66. ws2.Cells(row2, 1).Resize(1, 26).Value = temp
  67. rest = Arr(ix, 8) '受注残
  68. key = Arr(ix, 4) '車種
  69. 'Aシートに未登録なら終了
  70. If dicST.exists(key) = False Then
  71. GoTo FIN99
  72. End If
  73. '該当車種の行を繰り返す
  74. For row1 = dicST(key) To dicEN(key)
  75. '在庫がない製造番号はスキップする
  76. If ws1.Cells(row1, RESTCOL).Value = 0 Then GoTo NEXT99
  77. '受注残が0なら終了
  78. If rest = 0 Then Exit For
  79. ws2.Cells(row2, "J").Value = ws1.Cells(row1, "C").Value '製造NO
  80. If ws1.Cells(row1, RESTCOL).Value >= rest Then
  81. ws2.Cells(row2, "K").Value = rest '在庫
  82. ws1.Cells(row1, RESTCOL).Value = ws1.Cells(row1, RESTCOL).Value - rest '在庫残
  83. rest = 0 '受注残
  84. Else
  85. ws2.Cells(row2, "K").Value = ws1.Cells(row1, RESTCOL).Value '在庫
  86. rest = rest - ws1.Cells(row1, RESTCOL).Value '受注残
  87. ws1.Cells(row1, RESTCOL).Value = 0 '在庫残
  88. End If
  89. row2 = row2 + 1
  90. NEXT99:
  91. Next
  92. FIN99:
  93. row2 = row2 + 1
  94. End Sub
  95.  
  96.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty