关于excel:如果符合条件则计算唯一值

Count unique values if criteria is met

Excel电子表格:

1
2
3
4
5
6
7
8
9
10
11
12
            A              B           C         D         E
    1   Products         Sales       Count:      1
    2   Product_A         500      
    3   Product_A         400      
    4   Product_A          
    5   Product_B         200      
    6   Product_B          
    7   Product_C          
    8   Product_C          
    9   Product_C  
   10
   11

在上面的Excel电子表格中,我有products及其sales的列表。
如您所见,products可以在列表中出现多次,并且并非每个条目都具有一个sale,这会导致Column B中的empty cells

现在,我要计算一下根本没有销售的单价products
在上述情况下,这仅仅是Product_C,因为其他products确实在Column B中至少有一笔销售。
因此,Cell D1中的预期结果是1

到目前为止,我已经了解了这个公式:

1
=COUNTIFS(B2:B9,"<>"&"")

但是,它会计算Column B中的所有"",而不会检查product在列表中其他位置是否具有非空条目。

您是否知道我该如何实现?


如果您拥有Excel O365,则可以尝试:

1
=COUNTA(UNIQUE(FILTER(A2:A9,COUNTIFS(A2:A9,A2:A9,B2:B9,">0")=0)))


唯一零计数

如果您对VBA不敏感,这里有一个灵活的UDF。 1-3行表明只要在Sales列中选择任何单元格作为第二个参数就足够了。 4-5行表明Products列不允许这样做。如果将第三个参数设置为TRUE,则行6-8显示允许只使用一个单元格指定Products列,只要它是第一个要检查的单元格即可。 8行显示了Product_D的计数方式。行9显示超出范围(0)时的行为。

enter image description here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Option Explicit

Function Unique0Count(UniqueRange As Range, ValueRange As Range, _
  Optional calculateLastCell As Boolean = False) As Long

    Dim dict As Object      ' Dictionary Object
    Dim Key As Variant      ' Dictionary Key (For Each Control Variable)
    Dim rng As Range        ' Unique Range, Unique Last Cell Range,
                            ' Value Range
    Dim Unique As Variant   ' Unique Array
    Dim Value As Variant    ' Value Array
    Dim i As Long           ' Unique/Value Array Elements (Rows) Counter

    ' Write values from Unique Range to Unique Array.
    If Not calculateLastCell Then
        Set rng = UniqueRange.Columns(1)
    Else
        Set rng = Columns(UniqueRange.Column).Find(What:="*", _
          LookIn:=xlFormulas, SearchDirection:=xlPrevious)
        If rng Is Nothing Then Exit Function ' No data in column.
        If rng.Row < UniqueRange.Row Then Exit Function
        Set rng = Range(UniqueRange.Cells(1), rng)
    End If
    Unique = rng
    ' Write values from Value Range to Value Array.
    Set rng = Cells(rng.Row, ValueRange.Column).Resize(rng.Rows.Count)
    Value = rng

    ' Create a reference to the Dictionary Object(Late Binding).
    Set dict = CreateObject("Scripting.Dictionary")
    ' Loop through elements (rows) of Unique Array.
    For i = 1 To UBound(Unique)
        ' Check if value in current row of Source Array is NOT"".
        If Unique(i, 1) <>"" Then
            ' Write values of Unque Array to the Key of the Dictionary
            ' and sum the corresponding values of Value Array for each
            ' unique element to the Dictionary.
            dict(Unique(i, 1)) = dict(Unique(i, 1)) + Value(i, 1)
        End If
    Next

    ' Calculate the number of items containing value 0.
    For Each Key In dict.keys
        If dict(Key) = 0 Then Unique0Count = Unique0Count + 1
    Next Key

End Function

D1中,数组公式(通过按Ctrl Shift Enter确认):

1
=SUM(--(FREQUENCY(IF(0+(COUNTIFS(A2:A9,A2:A9,B2:B9,"<>")=0),MATCH(A2:A9,A2:A9,0)),ROW(A2:A9)-ROW(A1))>0))

enter image description here