在Excel中计算唯一值

day*_*yan 11 excel vba excel-formula

我需要在excel中计算范围(C2:C2080)中的唯一值.谷歌搜索公式:

=SUM(IF(FREQUENCY(MATCH(C2:C2080;C2:C2080;0);MATCH(C2:C280;C2:C2080;0))>0;1)) 
Run Code Online (Sandbox Code Playgroud)

返回不正确的值.

UPD:Lame解决方案:

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
Run Code Online (Sandbox Code Playgroud)

小智 25

这是一个适合我的VBA功能.

您可以将它用作工作表函数,引用任何范围,例如"= CountUnique(N8:O9)"

它处理文本和数值,并将空白单元格视为一个值

它不需要处理数组函数.

对于字典对象,它需要引用Microsoft Scripting Library .

    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
Run Code Online (Sandbox Code Playgroud)


Jac*_*cob 8

尝试:

=SUM(IF(FREQUENCY(C2:C2080,C2:C2080)>0,1))

编辑:以上处理列中的空白条目

  • 当尝试这个时,它只给我一个 0 的答案 (2认同)

小智 8


=SUM(IF(FREQUENCY(IF(LEN(A2:A10)>0,MATCH(A2:A10,A2:A10,0),""), IF(LEN(A2:A10)>0,MATCH(A2:A10,A2:A10,0),""))>0,1)) 
Run Code Online (Sandbox Code Playgroud)

http://office.microsoft.com/en-us/excel/HP030561181033.aspx

您也可以编写一个VBA宏(不确定这是否是您所追求的.)

实现的效果(给定A1-A11已填充且B1-B11为空的电子表格):

Sub CountUnique()

Dim count As Integer
Dim i, c, j As Integer

c = 0
count = 0
For i = 1 To 11
    Sheet1.Cells(i, 2).Value = Sheet1.Cells(i, 1).Value
    c = c + 1
    For j = 1 To c
        If CDbl(Sheet1.Cells(i, 1).Value) = CDbl(Sheet1.Cells(j, 2).Value) Then
            c = c - 1
            Exit For
        End If
    Next j
Next i

' c now equals the unique item count put in the 12'th row
Sheet1.Cells(12, 1).Value = c

End Sub
Run Code Online (Sandbox Code Playgroud)


Jim*_*m D 6

由于Excel中的某些限制,JustinG的功能非常好(并且快速),直到唯一项目的数量超过32,767.

我发现你修改了他的代码

Public Function CountUnique(rng As Range) As Integer
Run Code Online (Sandbox Code Playgroud)

并使它成为......

Public Function CountUnique(rng As Range) As Long
Run Code Online (Sandbox Code Playgroud)

然后它将处理更多独特的项目.


Jor*_*dan 5

对于仍在尝试使用@JustinG 的字典方法的任何人,如果您使用的是较新版本的 VBA,则需要稍微更改代码。

您需要引用“Microsoft Scripting Runtime”并在Dictionary术语前加上前缀Scripting,如下所示:

Public Function CountUnique(rng As Range) As Long
    Dim dict As Scripting.Dictionary
    Dim cell As Range
    Set dict = New Scripting.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
Run Code Online (Sandbox Code Playgroud)