Monday, May 5, 2008

Menghilangkan Duplikasi Dalam Excel

Berikut adalah kode VBA untuk menghapus duplikasi data serta menghilangkan item yang tidak sama.

Gambar di bawah menunjukkan dua buah kolom yang berisi data. Kita ingin menghapus duplikasi data dari kolom kedua dengan membandingkan ke isi kolom A.



Kode VBAnya adalah sebagai berikut:

Sub RemoveDuplicatesInCells()

    Dim c As Range
    Dim rCheckForDupes As Range

    Set rCheckForDupes = Range("A1:B14")

    With rCheckForDupes

        For Each c In .Columns(2).Cells

            If WorksheetFunction.CountIf(.Columns(1), c.Value) > 0 Then
                c.ClearContents
            End If

        Next c

    End With

    Set rCheckForDupes = Nothing

End Sub


Hasilnya adalah sebagai berikut:



Bagaimana untuk menghapus sel-sel yang kosong? Maka kita ubah kode VBA menjadi

.

Sub RemoveDuplicatesInCells2()

    Dim c As Range
    Dim rCheckForDupes As Range

    Set rCheckForDupes = Range("A1:B14")

    With rCheckForDupes

        For Each c In .Columns(2).Cells

            If WorksheetFunction.CountIf(.Columns(1), c.Value) > 0 Then
                c.ClearContents
            End If

        Next c

        .Columns(2).SpecialCells(xlCellTypeBlanks).Select

        Selection.Delete Shift:=xlUp

        .Cells(1).Select

    End With

    Set rCheckForDupes = Nothing

End Sub


Hasilnya adalah:.



Untuk menghapus data yang tidak sama maka, ubahlah tanda lebih besar (>) dalam "If WorksheetFunction.CountIf(.Columns(1), c.Value) > 0" menjadi tanda sama dengan (=) sign.


 

Sumber: andrewsexceltips.com.

No comments:

OfficeUsers.ORG Editorials