fork download
  1. Option Explicit
  2. Public Sub 在庫算出()
  3. Dim ws1 As Worksheet '購入品/使用品
  4. Dim ws2 As Worksheet '在庫
  5. Dim dicT1 As Object '箱数
  6. Dim dicT2 As Object '個包装
  7. Dim maxrow1 As Long
  8. Dim maxrow2 As Long
  9. Dim key As Variant
  10. Dim wrow As Long
  11. Set ws1 = Worksheets("Sheet1")
  12. Set ws2 = Worksheets("Sheet2")
  13. ws2.Range("A3:C" & Rows.Count).ClearContents
  14. Set dicT1 = CreateObject("Scripting.Dictionary")
  15. Set dicT2 = CreateObject("Scripting.Dictionary")
  16. maxrow1 = ws1.Cells(Rows.Count, "D").End(xlUp).Row
  17. maxrow2 = ws1.Cells(Rows.Count, "G").End(xlUp).Row
  18. '購入品登録
  19. For wrow = 3 To maxrow1
  20. key = ws1.Cells(wrow, "D").Value
  21. If dicT1.exists(key) = False Then
  22. dicT1(key) = 0
  23. dicT2(key) = 0
  24. End If
  25. dicT1(key) = dicT1(key) + ws1.Cells(wrow, "E").Value '箱数
  26. dicT2(key) = dicT2(key) + ws1.Cells(wrow, "F").Value '個包装
  27. Next
  28. '使用品引き算
  29. For wrow = 3 To maxrow2
  30. key = ws1.Cells(wrow, "G").Value
  31. If dicT1.exists(key) = False Then
  32. MsgBox (key & "は購入品になし")
  33. ws1.Activate
  34. ws1.Cells(wrow, "G").Select
  35. Exit Sub
  36. End If
  37. dicT1(key) = dicT1(key) - ws1.Cells(wrow, "H").Value '箱数
  38. dicT2(key) = dicT2(key) - ws1.Cells(wrow, "I").Value '個包装
  39. Next
  40. '在庫
  41. wrow = 3
  42. For Each key In dicT1.keys()
  43. ws2.Cells(wrow, "A").Value = key
  44. If dicT1(key) <> 0 Then
  45. ws2.Cells(wrow, "B").Value = dicT1(key)
  46. End If
  47. If dicT2(key) <> 0 Then
  48. ws2.Cells(wrow, "C").Value = dicT2(key)
  49. End If
  50. wrow = wrow + 1
  51. Next
  52. MsgBox ("完了")
  53. End Sub
  54.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty