remove duplicates from collection vba code example

Example: 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