fork download
  1. Option Explicit
  2. Public Sub 重複行削除()
  3. Dim maxrow As Long
  4. Dim ws As Worksheet
  5. Dim dicT As Object
  6. Dim wrow As Long '処理中行
  7. Dim trow As Long '重複登録行(残す行)
  8. Dim del_row As Long '削除対象行
  9. Dim key As String 'キー(分類1+分類2)
  10. Dim ctr As Long '削除行件数
  11. Dim drows As Range '削除対象行の集まり
  12. Set dicT = CreateObject("Scripting.Dictionary")
  13. Set ws = ActiveSheet
  14. maxrow = ws.Cells(Rows.Count, "B").End(xlUp).Row 'B列の最大行取得
  15. ctr = 0
  16. For wrow = 2 To maxrow
  17. key = ws.Cells(wrow, "D").Value & "|" & ws.Cells(wrow, "D").Value
  18. If dicT.exists(key) = False Then
  19. '最初のキー出現時(重複無の場合)
  20. dicT(key) = wrow
  21. Else
  22. '以降のキー出現時(重複有の場合)
  23. trow = dicT(key)
  24. If ws.Cells(wrow, "C").Value < ws.Cells(trow, "C").Value Then
  25. '登録済み商品の個数が多い場合
  26. del_row = wrow '当該行を削除
  27. ws.Cells(trow, "A").Value = ws.Cells(wrow, "B").Value '削除商品記入
  28. Else
  29. '登録済み商品の個数が小さい場合
  30. del_row = trow '登録済み商品の行を削除
  31. ws.Cells(wrow, "A").Value = ws.Cells(trow, "B").Value '削除商品記入
  32. dicT(key) = wrow '登録済み商品の行を書き換え
  33. End If
  34. '削除対象行を登録
  35. If ctr = 0 Then
  36. Set drows = Rows(del_row)
  37. Else
  38. Set drows = Union(drows, Rows(del_row))
  39. End If
  40. ctr = ctr + 1
  41. End If
  42. Next
  43. '削除対象行があるなら削除
  44. If ctr > 0 Then
  45. drows.Delete
  46. End If
  47. MsgBox ("完了")
  48. End Sub
  49.  
  50.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty