fork download
  1. Option Explicit
  2.  
  3. Sub 重複データを削除し合算()
  4.  
  5. Dim myDic As Object
  6. Dim myDic2 As Object
  7. Dim ws As Worksheet
  8. Dim maxrow As Long
  9. Dim wrow As Long
  10. Dim wrow2 As Long
  11. Dim Target As Variant
  12. Dim tmp As Variant
  13. Application.ScreenUpdating = False
  14. Set ws = ActiveSheet
  15. Set myDic = CreateObject("Scripting.Dictionary")
  16. Set myDic2 = CreateObject("Scripting.Dictionary")
  17. '最大行取得
  18. maxrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  19. '出力先クリア
  20. ws.Range("H2:M" & Rows.Count).ClearContents
  21. For wrow = 2 To maxrow
  22. Target = Cells(wrow, "A").Value & "|" & Cells(wrow, "B").Value & "|" & Cells(wrow, "C").Value
  23. If myDic.exists(Target) = False Then
  24. 'キーが最初に出現した場合
  25. myDic(Target) = Cells(wrow, "D").Value
  26. myDic2(Target) = wrow
  27. Else
  28. 'キーが出現済みの場合
  29. myDic(Target) = myDic(Target) + Cells(wrow, "D").Value
  30. End If
  31. Next
  32. wrow2 = 2
  33. '全キーを処理する
  34. For Each Target In myDic.keys
  35. tmp = Split(Target, "|")
  36. ws.Cells(wrow2, "H").Value = tmp(0) '商品名
  37. ws.Cells(wrow2, "I").Value = tmp(1) 'カテゴリ
  38. ws.Cells(wrow2, "J").Value = tmp(2) '産地
  39. ws.Cells(wrow2, "K").Value = myDic(Target) '個数
  40. wrow = myDic2(Target)
  41. ws.Cells(wrow2, "L").Value = ws.Cells(wrow, "E").Value '品番
  42. ws.Cells(wrow2, "M").Value = ws.Cells(wrow, "F").Value '品目
  43. wrow2 = wrow2 + 1
  44. Next
  45. Application.ScreenUpdating = True
  46.  
  47. End Sub
  48.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty