简体   繁体   English

Excel在许多情况下的非重复计数

[英]Excel distinct count with many conditions

  A            B       C   D  E       F           G           H   I  
115492          2009    10  42  5   90160624    15-Oct-09       WH  5
115492          2009    10  42  5   90160624    15-Oct-09       WH  5
115492          2009    10  42  5   90160624    15-Oct-09       WH  5
115492          2009    10  42  5   90160624    15-Oct-09       WH  5
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115481          2009    10  44  6   90170587    30-Oct-09       WH  3
115520          2009    11  45  5   90174693    5-Nov-09        WH  3
115520          2009    11  45  5   90174693    5-Nov-09        WH  3
115520          2009    11  45  5   90174693    5-Nov-09        WH  3
115520          2009    11  45  5   90174693    5-Nov-09        WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
115502          2009    11  46  6   90179821    13-Nov-09       WH  3
123057          2009    11  46  3   90182107    17-Nov-09       WH  3
123057          2009    11  46  3   90182107    17-Nov-09       WH  3
123057          2009    11  46  3   90182107    17-Nov-09       WH  3
123057          2009    11  46  3   90182107    17-Nov-09       WH  3
123056          2009    11  47  3   90186948    24-Nov-09       WH  3
123056          2009    11  47  3   90186948    24-Nov-09       WH  3
123056          2009    11  47  3   90186948    24-Nov-09       WH  3
123056          2009    11  47  3   90186948    24-Nov-09       WH  3
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115506          2009    11  47  3   90186673    24-Nov-09       WH  5
115496          2009    11  47  4   90187409    25-Nov-09       WH  3
115496          2009    11  47  4   90187409    25-Nov-09       WH  3
115496          2009    11  47  4   90187409    25-Nov-09       WH  3
115496          2009    11  47  4   90187409    25-Nov-09       WH  3
123058          2009    12  50  5   90198449    10-Dec-09       WH  3
123058          2009    12  50  5   90198449    10-Dec-09       WH  3
123058          2009    12  50  5   90198449    10-Dec-09       WH  3
123058          2009    12  50  5   90198449    10-Dec-09       WH  3
115522          2009    12  50  7   90200094    12-Dec-09       WH  7
115522          2009    12  50  7   90200094    12-Dec-09       WH  7
115522          2009    12  50  7   90200094    12-Dec-09       WH  7
115522          2009    12  50  7   90200094    12-Dec-09       WH  7
115522          2009    12  50  7   90200094    12-Dec-09       WH  7
123059          2009    12  51  6   90203897    18-Dec-09       WH  4
123059          2009    12  51  6   90203897    18-Dec-09       WH  4
123059          2009    12  51  6   90203897    18-Dec-09       WH  4
123059          2009    12  51  6   90203897    18-Dec-09       WH  4
115539          2009    12  51  7   90204074    19-Dec-09       WH  5
115539          2009    12  51  7   90204074    19-Dec-09       WH  5
115539          2009    12  51  7   90204074    19-Dec-09       WH  5
115539          2009    12  51  7   90204074    19-Dec-09       WH  5
115539          2009    12  51  7   90204074    19-Dec-09       WH  5
115539          2009    12  51  7   90204074    19-Dec-09       WH  5
115541          2009    12  51  7   90204389    19-Dec-09       WH  4
115541          2009    12  51  7   90204389    19-Dec-09       WH  4
115541          2009    12  51  7   90204389    19-Dec-09       WH  4
115541          2009    12  51  7   90204389    19-Dec-09       WH  4
115534          2009    12  52  2   90209074    28-Dec-09       WH  5
115534          2009    12  52  2   90209074    28-Dec-09       WH  5
115534          2009    12  52  2   90209074    28-Dec-09       WH  5
115534          2009    12  52  2   90209074    28-Dec-09       WH  5
115534          2009    12  52  2   90209074    28-Dec-09       WH  5
115534          2009    12  52  2   90209074    28-Dec-09       WH  5

I need to count the number of unique entries in column 'A' where column 'I' equals 3: I achieved this by following: 我需要计算“ A”列中唯一条目的数量,其中“ I”列等于3:我是通过以下方式实现的:

=SUM(IF(FREQUENCY(A2:A76,A2:A76)>0,(I2:I76=3)*1,0))

What if I need to add more conditions like "where column 'I' equals 3 and column 'C' equals 11"? 如果我需要添加更多条件,例如“列'I'等于3,列'C'等于11”怎么办?

I tried this: 我尝试了这个:

=SUM(IF(FREQUENCY(A2:A76,A2:A76)>0,(AND(I2:I76=3, C2:C76 = 11))*1,0))

But, this formula doesn't give me the correct number. 但是,这个公式不能给我正确的数字。 what would be the right formula? 正确的公式是什么?

Multiply the conditions this way: 这样乘以条件:

=SUM(IF(FREQUENCY(A2:A76,A2:A76)>0,(I2:I76=3)*(C2:C76=11)*1,0))

This needs to be an array fomula (Ctrl+Shift+Enter after entering the formula). 这必须是一个数组公式(输入公式后按Ctrl + Shift + Enter)。 (I guess you would already know that considering your function in the question returned a value) (我想您已经知道考虑问题中的函数返回了一个值)

In my experience these type of formulat's tend to get excessively complicated and totally uncomprehensible for others. 根据我的经验,这些类型的公式往往变得过于复杂,而其他人则完全无法理解。 To create the proper result and keep the whole formula understandable I usually split it up into a couple intermediate columns. 要创建正确的结果,并保持整个公式可以理解的,我通常把它分解成几个中间列。

In your case you could: 在您的情况下,您可以:

  • Add a column, say X that has a 1 if I equals 3 otherwise a 0 添加一列,如果X等于3,则X等于1,否则等于0
  • Add a column say Y that has a 1 if C equals 11 otherwise a 0 添加一列,说Y如果C等于11则为1,否则为0
  • Add a 'total' column that determines if all are true as =X*Y 添加一个“总计”列,确定所有条件是否均满足= X * Y
  • Then do your sumif 然后做你的总结

This can be extended as far as you like and keeps the whole proces traceable. 可以将其扩展到您希望的程度,并使整个过程可追溯。 And if it is of any importance you can always hide the intermediate columns from view 并且,如果有任何重要意义,您始终可以从视图中隐藏中间列

I know you haven't asked for VBA and there are ways to solve this without it, but I really feel that VBA excels in this case since the code is so easy to manage and you can easily add/delete conditions for counting. 我知道您没有要求VBA,没有它也可以解决此问题,但是我真的觉得VBA在这种情况下很出色,因为代码是如此易于管理,并且您可以轻松添加/删除计数条件。

Sub SpecialCount()

Dim vArray As Variant
Dim lastRow As Long
Dim i As Long
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")

lastRow = range("A" & Rows.count).End(xlUp).Row
vArray = range("A1", "I" & lastRow).Value

On Error Resume Next
For i = 1 To UBound(vArray, 1)
    'Condition 1
    If vArray(i, 9) = 3 Then
        'Condition 2
        If vArray(i, 3) = 11 Then
            dict.Add vArray(i, 1), 1
        End If
    End If
Next

MsgBox dict.count & " unique entries found."
End Sub

How it works : First it finds the last cell in columnn A. I then dump the entire range of cells into a variant array since they are really fast to work with. 工作原理 :首先,它在列n中找到最后一个单元。然后,我将整个范围的单元都转储到变量数组中,因为它们的处理速度非常快。 Then it just loops through each colmumn A value and looks at the value in the 9th row (I) and 3rd row (C), and if they match your conditions, the entry from "A" is added to a dicitonary object. 然后,它仅循环浏览每个库仑A值,并查看第9行(I)和第3行(C)中的值,如果它们符合您的条件,则将“ A”中的条目添加到一个数字对象中。 Since dictionaries can only hold one of each unique item, it automatically gets you the unique count! 由于词典只能容纳每个唯一项,因此它会自动为您获得唯一计数! Then I just display a message box telling you the result. 然后,我只显示一个消息框,告诉您结果。

Please check it out - I hope that I'm not the only one who appreciates how easy VBA is to mangae and alter. 请检查一下-我希望我不是唯一一个欣赏VBA漫画和改动多么容易的人。 BTW, there are 5 unique entries in that list you posted in which I is 3 and C is 11. 顺便说一句,您发布的列表中有5个唯一条目,其中I为3,C为11。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM