I need to count unique values in range (C2:C2080) in excel. Googled formula:
=SUM(IF(FREQUENCY(MATCH(C2:C2080;C2:C2080;0);MATCH(C2:C280;C2:C2080;0))>0;1))
return incorrect value.
UPD: Lame solution:
Sub CountUnique()
Dim i, count, j As Integer
count = 1
For i = 1 To 470
flag = False
If count > 1 Then
For j = 1 To count
If Sheet1.Cells(i, 3).Value = Sheet1.Cells(j, 11).Value Then
flag = True
End If
Next j
Else
flag = False
End If
If flag = False Then
Sheet1.Cells(count, 11).Value = Sheet1.Cells(i, 3).Value
count = count + 1
End If
Next i
Sheet1.Cells(1, 15).Value = count
End Sub
This might be a more efficient way of dealing with a large number of rows. This uses the inbuilt AdvancedFilter command instead of cycling through each cell at a time.
JustinG's function works very well (and fast) until the number of unique items exceeds 32,767 due to some type of limit in Excel.
I found if you modify his code
and make it as...
It will then handle more unique items.
You could also simply use a filter to temporarily display unique values, and count the filtered values.
http://office.microsoft.com/en-us/excel/HP030561181033.aspx
You may also write a VBA macro (not sure if that's what you're after though.)
Something to the effect of (given a spreadsheet with A1-A11 filled and B1-B11 empty):
After reading through this and then investigating further, I've got one that works better for me than anything I see here:
Array-enter:
(Ctrl+Shift+Enter, and don't include the curly brackets)
Or in VBA:
It works for both numbers and text, it handles blank cells, it handles errors in referenced cells, and it works in VBA. It's also one of the most compact solutions I've seen. Using it in VBA, it apparently automatically handles the need to be an array formula.
Note, the way it handles errors is by simply including them in the count of uniques. For example, if you have two cells returning #DIV/0! and three cells returning #VALUE!, those 5 cells would add 2 to the final count of unique values. If you want errors completely excluded, it would need to be modified for that.
In my tests, this one from Jacob above only works for numbers, not text, and does not handle errors in referenced cells (returns an error if any of the referenced cells returns an error):
Here is a VBA function that works for me.
You can use it as a worksheet function, referencing any range, eg “=CountUnique(N8:O9)”
It handles text and numeric values, and treats blank cells as one value
It does not require dealing with array functions.
It requires a reference to the Microsoft Scripting Library, for the dictionary object.