Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Collection does not work with strings?

Hi all coders out there!

I wanted a function that returned the number of unique elements in a
range.
And I thought that the best and easyest solution would be using the
Collection object.
But to my big surprice the solution I came up with only worked with
numbers!

Option Explicit

' Returns the number of unique values in the given range.
' Only works with numbers. why not strings?
Public Function uniqueCountInt(r As Range)
Dim tmpR As Range
Dim tmpColl As New Collection
On Error Resume Next

For Each tmpR In r.Cells
tmpColl.Item tmpR.Cells.Value

If Err.Number < 0 Then
tmpColl.Add tmpR.Cells.Value
End If
Err.Clear
Next

uniqueCountInt = tmpColl.Count
End Function


Does any one out there have a tip on the easyest way of making the
function work with strings (and numbersc as well if possible)?
Is Collection the right way to do it?

best regards/
Olaf
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Collection does not work with strings?

Sub AAAA()
Dim rng As Range
Set rng = Range("A1:a15")
Debug.Print uniqueCountInt(rng)
End Sub

Public Function uniqueCountInt(r As Range)
Dim tmpR As Range
Dim tmpColl As New Collection
On Error Resume Next

For Each tmpR In r.Cells
tmpColl.Add tmpR.Value, CStr(tmpR.Value)
Next
On Error GoTo 0
uniqueCountInt = tmpColl.Count
End Function


Worked fine for me with strings. Note that is was case insensitive.

--
Regards,
Tom Ogilvy

"Olaf" wrote in message
om...
Hi all coders out there!

I wanted a function that returned the number of unique elements in a
range.
And I thought that the best and easyest solution would be using the
Collection object.
But to my big surprice the solution I came up with only worked with
numbers!

Option Explicit

' Returns the number of unique values in the given range.
' Only works with numbers. why not strings?
Public Function uniqueCountInt(r As Range)
Dim tmpR As Range
Dim tmpColl As New Collection
On Error Resume Next

For Each tmpR In r.Cells
tmpColl.Item tmpR.Cells.Value

If Err.Number < 0 Then
tmpColl.Add tmpR.Cells.Value
End If
Err.Clear
Next

uniqueCountInt = tmpColl.Count
End Function


Does any one out there have a tip on the easyest way of making the
function work with strings (and numbersc as well if possible)?
Is Collection the right way to do it?

best regards/
Olaf



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Collection does not work with strings?

Olaf,
You need to add the string value as a key. Indeed the key only accepts
string, not numeric variable (unless you CStr first)

From the help file:
MyClasses.Add item := Inst, key := CStr(Num)

NickHK

"Olaf" wrote in message
om...
Hi all coders out there!

I wanted a function that returned the number of unique elements in a
range.
And I thought that the best and easyest solution would be using the
Collection object.
But to my big surprice the solution I came up with only worked with
numbers!

Option Explicit

' Returns the number of unique values in the given range.
' Only works with numbers. why not strings?
Public Function uniqueCountInt(r As Range)
Dim tmpR As Range
Dim tmpColl As New Collection
On Error Resume Next

For Each tmpR In r.Cells
tmpColl.Item tmpR.Cells.Value

If Err.Number < 0 Then
tmpColl.Add tmpR.Cells.Value
End If
Err.Clear
Next

uniqueCountInt = tmpColl.Count
End Function


Does any one out there have a tip on the easyest way of making the
function work with strings (and numbersc as well if possible)?
Is Collection the right way to do it?

best regards/
Olaf



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
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
CONCATENATE I have two text strings in cells but it wont work paintsr Excel Discussion (Misc queries) 1 January 23rd 09 04:30 PM
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
work with strings/numbers and dates William DeLeo Excel Programming 2 May 7th 04 02:32 PM
Using a collection class to implement mutliple find/replace strings in cells Bill Hertzing Excel Programming 2 February 18th 04 01:42 AM


All times are GMT +1. The time now is 05:48 AM.

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

About Us

"It's about Microsoft Excel"