Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Unique values in a Collection

I am trying to add values to a collection to display unique values. I have a
worksheet with comma separated values in a of column of cells. I am using the
Split function to loop through the values and add them to the Collection.
There are duplicate values in the string, I do not understand why they are
still added to the Collection.

Public Function RemoveDupes(c As Range) As String
Dim i As Integer
Dim itm
Dim strItems As String
Dim nStr() As String
Dim nColl As New Collection

nStr = Split(c, ",")
For i = 0 To UBound(nStr)
nColl.Add Item:=nStr(i)
Next

For Each itm In nColl
Debug.Print itm
strItems = strItems & itm & ","
Next

If Not Len(strItems) = 0 Then
RemoveDupes = Left(strItems, Len(strItems) - 1)
Else
RemoveDupes = ""
End If
Set nColl = Nothing
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Unique values in a Collection

A Collection may contain duplicate values but not duplicate keys. Try
something like this

Sub test()
Dim i As Long
Dim s As String
Dim sa() As String
Dim col As Collection

s = "a,b,a,c,a,d"
sa = Split(s, ",")

Set col = New Collection
On Error Resume Next
For i = 0 To UBound(sa)
col.Add sa(i), sa(i)
Next

For i = 1 To col.Count
Debug.Print col(i)
Next

End Sub

Regards,
Peter T


"Ralph" wrote in message
...
I am trying to add values to a collection to display unique values. I have
a
worksheet with comma separated values in a of column of cells. I am using
the
Split function to loop through the values and add them to the Collection.
There are duplicate values in the string, I do not understand why they are
still added to the Collection.

Public Function RemoveDupes(c As Range) As String
Dim i As Integer
Dim itm
Dim strItems As String
Dim nStr() As String
Dim nColl As New Collection

nStr = Split(c, ",")
For i = 0 To UBound(nStr)
nColl.Add Item:=nStr(i)
Next

For Each itm In nColl
Debug.Print itm
strItems = strItems & itm & ","
Next

If Not Len(strItems) = 0 Then
RemoveDupes = Left(strItems, Len(strItems) - 1)
Else
RemoveDupes = ""
End If
Set nColl = Nothing
End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Unique values in a Collection

Thank you! I had been fooling with that for hours. Made the change, works
like a charm now.

"Peter T" wrote:

A Collection may contain duplicate values but not duplicate keys. Try
something like this

Sub test()
Dim i As Long
Dim s As String
Dim sa() As String
Dim col As Collection

s = "a,b,a,c,a,d"
sa = Split(s, ",")

Set col = New Collection
On Error Resume Next
For i = 0 To UBound(sa)
col.Add sa(i), sa(i)
Next

For i = 1 To col.Count
Debug.Print col(i)
Next

End Sub

Regards,
Peter T


"Ralph" wrote in message
...
I am trying to add values to a collection to display unique values. I have
a
worksheet with comma separated values in a of column of cells. I am using
the
Split function to loop through the values and add them to the Collection.
There are duplicate values in the string, I do not understand why they are
still added to the Collection.

Public Function RemoveDupes(c As Range) As String
Dim i As Integer
Dim itm
Dim strItems As String
Dim nStr() As String
Dim nColl As New Collection

nStr = Split(c, ",")
For i = 0 To UBound(nStr)
nColl.Add Item:=nStr(i)
Next

For Each itm In nColl
Debug.Print itm
strItems = strItems & itm & ","
Next

If Not Len(strItems) = 0 Then
RemoveDupes = Left(strItems, Len(strItems) - 1)
Else
RemoveDupes = ""
End If
Set nColl = Nothing
End Function




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default Unique values in a Collection

Hello,

You might want to get insured against empty cells/values:
coll.Add lngIndex, "X" & sPattern

See
http://sulprobil.com/html/lfreq.html
UDF List_Freq...

Regards,
Bernd
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 137
Default Unique values in a Collection

Well Done Peter. This is brilliant! [And QUICK!]
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unique Values, not Unique Records steph44haf Excel Discussion (Misc queries) 1 May 12th 10 07:52 PM
Adding Unique Items to a Collection WJ Excel Discussion (Misc queries) 2 June 12th 07 03:46 PM
Collection of case sensitive unique items Alan Beban[_2_] Excel Programming 4 October 28th 04 08:20 PM
Add Unique item to Collection Dick Kusleika[_3_] Excel Programming 8 January 21st 04 08:59 PM
Compare Listbox values with Collection values Stuart[_5_] Excel Programming 2 September 20th 03 01:58 PM


All times are GMT +1. The time now is 06:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"