fork download
  1. Option Explicit
  2. Dim row3 As Long
  3. Dim ws1 As Worksheet
  4. Dim ws2 As Worksheet
  5. Dim ws3 As Worksheet
  6. Dim dicST As Object
  7. Dim dicEN As Object
  8. Public Sub 転記()
  9. Dim maxrow1 As Long
  10. Dim maxrow2 As Long
  11. Dim row1 As Long
  12. Dim row2 As Long
  13. Dim key As String '車種
  14. Set ws1 = Sheets("在庫一覧")
  15. Set ws2 = Sheets("まとめ")
  16. Set ws3 = Sheets("転記先")
  17. Set dicST = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  18. Set dicEN = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  19. maxrow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row 'A列の最大行取得
  20. maxrow2 = ws2.Cells(Rows.Count, 2).End(xlUp).Row 'B列の最大行取得
  21. If maxrow1 < 5 Then Exit Sub
  22. If maxrow2 < 6 Then Exit Sub
  23. ws3.Rows("6:" & Rows.Count).ClearContents
  24. row3 = 6
  25. '車種を取り込む
  26. For row1 = 5 To maxrow1
  27. key = ws1.Cells(row1, "A").Value
  28. If dicST.exists(key) = False Then
  29. dicST(key) = row1
  30. dicEN(key) = row1
  31. Else
  32. If dicEN(key) + 1 <> row1 Then
  33. ws1.Activate
  34. ws1.Cells(row1, "A").Select
  35. MsgBox ("車種が連続していません")
  36. Exit Sub
  37. End If
  38. dicEN(key) = row1
  39. End If
  40. Next
  41. For row2 = 6 To maxrow2
  42. key = ws2.Cells(row2, "B").Value
  43. If dicST.exists(key) = False Then
  44. ws2.Activate
  45. ws2.Cells(row2, "B").Select
  46. MsgBox ("車種が在庫一覧にありません")
  47. Exit Sub
  48. End If
  49. Call tenki(key, row2)
  50. dicST.Remove key
  51. dicEN.Remove key
  52. Next
  53. 'ソート用キーをクリア
  54. ws3.Range("K6:L" & Rows.Count).ClearContents
  55. MsgBox ("完了")
  56. End Sub
  57.  
  58. Private Sub tenki(ByVal key As String, ByVal row2 As Long)
  59. Dim wrow As Long
  60. Dim rcnt As Long
  61. Dim srow1 As Long
  62. Dim wrow3 As Long
  63. Dim i As Long
  64. Dim ratio As Double
  65. Dim m_weight As Variant '必要重量
  66. Dim p_weight As Variant '部品重量
  67. rcnt = dicEN(key) - dicST(key) + 1
  68. srow1 = dicST(key)
  69. For i = 0 To rcnt - 1
  70. '車種、型式、必要重量
  71. ws3.Cells(row3 + i, "B").Resize(1, 3).Value = ws2.Cells(row2, "B").Resize(1, 3).Value
  72. '製造No、部品重量
  73. ws3.Cells(row3 + i, "E").Resize(1, 2).Value = ws1.Cells(srow1 + i, "C").Resize(1, 2).Value
  74. '部品単量
  75. ws3.Cells(row3 + i, "G").Value = ws2.Cells(row2, "G").Value
  76. '不良品
  77. ws3.Cells(row3 + i, "H").Value = ws1.Cells(srow1 + i, "F").Value
  78. Next
  79. '判定処理
  80. For i = 0 To rcnt - 1
  81. If ws3.Cells(row3 + i, "H").Value = "不良" Then
  82. ws3.Cells(row3 + i, "A").Value = "他"
  83. ws3.Cells(row3 + i, "i").Value = "なし" '結果
  84. ws3.Cells(row3 + i, "K").Value = 9 'ソート用第1キー
  85. ws3.Cells(row3 + i, "L").Value = 0 'ソート用第2キー
  86. Else
  87. m_weight = ws3.Cells(row3 + i, "D").Value
  88. p_weight = ws3.Cells(row3 + i, "F").Value
  89. If IsNumeric(m_weight) = False Then
  90. ws3.Activate
  91. ws3.Cells(row3 + i, "D").Select
  92. MsgBox ("必要重量が不正です")
  93. End
  94. End If
  95. If m_weight = 0 Then
  96. ws3.Activate
  97. ws3.Cells(row3 + i, "D").Select
  98. MsgBox ("必要重量が0です")
  99. End
  100. End If
  101. If IsNumeric(p_weight) = False Then
  102. ws3.Activate
  103. ws3.Cells(row3 + i, "F").Select
  104. MsgBox ("部品重量が不正です")
  105. End
  106. End If
  107. ratio = (p_weight - m_weight) / m_weight
  108. If Abs(ratio) <= 0.1 Then
  109. ws3.Cells(row3 + i, "A").Value = "○" '状態
  110. ws3.Cells(row3 + i, "K").Value = 1 'ソート用第1キー
  111. Else
  112. ws3.Cells(row3 + i, "A").Value = "他" '状態
  113. ws3.Cells(row3 + i, "i").Value = "なし" '結果
  114. ws3.Cells(row3 + i, "K").Value = 2 'ソート用第1キー
  115. End If
  116. ws3.Cells(row3 + i, "L").Value = Abs(ratio) 'ソート用第2キー
  117. End If
  118. Next
  119. 'ソート
  120. ws3.Range("A" & row3 & ":L" & row3 + rcnt - 1).Sort key1:=ws3.Range("K" & row3), key2:=ws3.Range("L" & row3)
  121. '車種、型式、必要重量の重複部、部品単量をクリア
  122. For i = 1 To rcnt - 1
  123. ws3.Cells(row3 + i, "B").Resize(1, 3).Value = ""
  124. ws3.Cells(row3 + i, "G").Value = ""
  125. Next
  126. row3 = row3 + rcnt + 1
  127. End Sub
  128.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty