VBA remove duplicates from array code example

Example 1: vba code to remove duplicates from a column

Sub removeDuplicate()
 'removeDuplicate Macro
 Columns("A:A").Select
 ActiveSheet.Range("$A$1:$A$117").RemoveDuplicates Columns:=Array(1), _ 
 Header:=xlNo 
 Range("A1").Select
 End Sub

Example 2: vba code to remove duplicates from a column

Sub dedupe_abcd()
    Dim icol As Long

    With Sheets("Sheet1")   '<-set this worksheet reference properly!
        icol = Application.Match("abcd", .Rows(1), 0)
        With .Cells(1, 1).CurrentRegion
            .RemoveDuplicates Columns:=icol, Header:=xlYes
        End With
    End With
End Sub

Example 3: add items to collection without duplicates VBA

Sub Sample()
    Dim col As New Collection
	
  'The second part of the add statement defines the keys
  'Since we cannot have two keys, the item will not be added
  'When we try to add the same key, we get an error
  'that is why we need the "On error resume next"

    On Error Resume Next
    col.Add 111, Cstr(111)
    col.Add 222, Cstr(222)
    col.Add 111, Cstr(111)
    col.Add 111, Cstr(111)
    col.Add 333, Cstr(333)
    col.Add 111, Cstr(111)
    col.Add 444, Cstr(444)
    col.Add 555, Cstr(555)
    On Error GoTo 0

End Sub

Tags:

Vb Example