View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Don Guillett Don Guillett is offline
external usenet poster
 
Posts: 10,124
Default VBA in Counting Unique Values

How about a regular function.
=SUMPRODUCT((A1:A15<"")/COUNTIF(A1:A15,A1:A15&""))
or vba
Sub countunique()
Dim c As Range, UC As New Collection
On Error Resume Next
lr = Cells(Rows.Count, "a").End(xlUp).Row
For Each c In Range("a1:a" & lr)
UC.Add c.Value, CStr(c.Value)
Next c
'MsgBox UC.Count
Range("b8") = UC.Count
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Seeker" wrote in message
...
I need to count numerical & text from A2:A65536 in €śsheet B€ť and would like
to have the counting result shows in €śsheet A€ť cell A1. I found a VBA
posted
on 12 Jan 2007 which may fit my needs. However I am illiteracy to VBA and
dont know how to embed following VBA to my current one which was done by
macro recorder. I also need help in replacing information to fit the range
name & sheet name by using this VBA.
Thanks.

Function CountValues(TheRange As Range) As Integer
Dim SaveValues(99) As Variant
Dim Index As Integer
Dim MaxIndex As Integer
Dim FoundValue As Boolean
Index = 0
MaxIndex = 0
Dim cell As Range
For Each cell In TheRange
If IsNumeric(cell) Then
TheValue = Val(cell.Value)
If MaxIndex = 0 Then
SaveValues(MaxIndex) = TheValue
MaxIndex = MaxIndex + 1
Else
FoundValue = False
Index = 0
While Not FoundValue And Index <= MaxIndex
FoundValue = (SaveValues(Index) = TheValue)
If Not FoundValue Then Index = Index + 1
Wend
If Not FoundValue Then
SaveValues(MaxIndex) = TheValue
MaxIndex = MaxIndex + 1
End If
End If
End If
Next
CountValues = MaxIndex
End Function