View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default Eliminating Duplicates

One way of doing this.
Can't remember where I got the idea from to use a collection to remove
duplicates.
I don't think it is the fastest way, but it looks nice.
To test put the data in cells A1 to F1.

Function RemoveDuplicates(ByRef arr1 As Variant) As Variant

'removes duplicates from an Excel row, passed as an array
'--------------------------------------------------------

Dim NoDupes As Collection
Dim i As Long
Dim UB As Long
Dim arr2()

Set NoDupes = New Collection

UB = UBound(arr1, 2)

'The next statement ignores the error caused
'by attempting to add a duplicate key to the collection.
'The duplicate is not added - which is just what we want!
'--------------------------------------------------------
On Error Resume Next
For i = 1 To UB
NoDupes.Add arr1(1, i), CStr(arr1(1, i))
'Note: the 2nd argument (key) for
'the Add method must be a string
'--------------------------------
Next
On Error GoTo 0

ReDim arr2(1, 1 To NoDupes.Count)

'copy the collection to the new array
'------------------------------------
For i = 1 To NoDupes.Count
arr2(1, i) = NoDupes(i)
Next

RemoveDuplicates = arr2

End Function


Sub test()

Dim i As Long
Dim strUniques As String
Dim arr()

arr = Range(Cells(1), Cells(6))

arr = RemoveDuplicates(arr)

For i = 1 To UBound(arr, 2)
strUniques = strUniques & arr(1, i)
Next

Cells(8).Value = strUniques

End Sub


RBS


"Parker" wrote in message
...
I have 5 cells going across my spreadsheet, that contain
c c c j q respectively. I want to note, in another cell,
that J Q and C have occured, but I don't want the C to
show up 3 times. How do I do this?