我需要在 Excel 中计算范围 (C2:C2080) 中的唯一值。谷歌搜索公式:
=SUM(IF(FREQUENCY(MATCH(C2:C2080;C2:C2080;0);MATCH(C2:C280;C2:C2080;0))>0;1))
返回不正确的值。
UPD:蹩脚的解决方案:
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
这是一个适合我的 VBA 函数。
您可以将其用作工作表功能 http://msdn.microsoft.com/en-us/library/office/hh211481%28v=office.14%29.aspx,引用任何范围,例如“=CountUnique(N8:O9)”
它处理文本和数值,并将空白单元格视为一个值
它不需要处理数组函数。
它需要引用 Microsoft 脚本库,用于字典对象 http://support.microsoft.com/kb/187234.
Public Function CountUnique(rng As Range) As Integer
Dim dict As Dictionary
Dim cell As Range
Set dict = New Dictionary
For Each cell In rng.Cells
If Not dict.Exists(cell.Value) Then
dict.Add cell.Value, 0
End If
Next
CountUnique = dict.Count
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)