Django Girls and Boys 備忘録

Python、Selenium、Django、java、iPhoneアプリ、Excelマクロなどで気付いたこと、覚えておきたいことなどを載せていきます。

【Excel VBA 高速処理】大量データの重複削除マクロ

数千〜数万行のデータ。
「標準の重複削除が遅い」「条件付きで消したい」
そんな悩み、ありませんか?

ここでは VBAで高速に重複を削除する方法 を紹介します。


🔥 方法①:RemoveDuplicates(最速&簡単)

 
Sub RemoveDuplicateFast()

    With Worksheets("Sheet1")
        .Range("A1").CurrentRegion.RemoveDuplicates _
            Columns:=Array(1), Header:=xlYes
    End With

    MsgBox "重複削除が完了しました"

End Sub
 

✔ 数万行でも高速
✔ 単一キーならこれが最強


🧠 方法②:Dictionaryを使った超柔軟版(条件付き)

 
Sub RemoveDuplicateWithDictionary()

    Dim dict As Object
  Set dict = CreateObject("Scripting.Dictionary")

    Dim i As Long
    Dim lastRow As Long

    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = lastRow To 2 Step -1
        If dict.Exists(Cells(i, 1).Value) Then
            Rows(i).Delete
        Else
            dict.Add Cells(i, 1).Value, True
        End If
    Next i
    MsgBox "重複削除完了(Dictionary使用)"

End Sub

 

✔ 複数条件・加工後比較が可能
✔ 業務データ向き


✨ 使い分けの目安

状況 おすすめ
単純な重複削除 RemoveDuplicates
条件付き・複雑 Dictionary