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电子表格中,我有
如您所见,
现在,我要计算一下根本没有销售的单价
在上述情况下,这仅仅是
因此,
到目前为止,我已经了解了这个公式:
1 | =COUNTIFS(B2:B9,"<>"&"") |
但是,它会计算
您是否知道我该如何实现?
如果您拥有Excel O365,则可以尝试:
1 | =COUNTA(UNIQUE(FILTER(A2:A9,COUNTIFS(A2:A9,A2:A9,B2:B9,">0")=0))) |
唯一零计数
如果您对VBA不敏感,这里有一个灵活的UDF。
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 |
在
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)) |