Excel电子表格:
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
,这会导致empty cells
中的Column B
。
现在,我要计算完全没有销售的 unqiue products
。
在上述情况下,这仅仅是Product_C
,因为其他products
确实在Column B
中至少有一笔销售。
因此,Cell D1
中的预期结果是1
。
到目前为止,我已经了解了这个公式:
=COUNTIFS(B2:B9,"<>"&"")
但是,它会计算""
中的所有Column B
,而不会检查product
在列表中其他位置是否具有非空条目。
您知道我如何实现这一目标吗?
答案 0 :(得分:2)
如果您拥有Excel O365,则可以尝试:
=COUNTA(UNIQUE(FILTER(A2:A9,COUNTIFS(A2:A9,A2:A9,B2:B9,">0")=0)))
答案 1 :(得分:1)
在D1
中,数组公式(通过按Ctrl + Shift + Enter确认):
=SUM(--(FREQUENCY(IF(0+(COUNTIFS(A2:A9,A2:A9,B2:B9,"<>")=0),MATCH(A2:A9,A2:A9,0)),ROW(A2:A9)-ROW(A1))>0))
答案 2 :(得分:1)
如果您对VBA不敏感,这里有一个灵活的UDF。第1-3
行表明只要在Sales
列中选择任何单元格作为第二个参数就足够了。 4-5
行显示Products
列不允许这样做。如果将第三个参数设置为TRUE
,则行6-8
表示允许使用仅一个单元格指定Products
列,只要它是第一个要检查的单元格即可。第8
行显示如何不计算Product_D
。第9
行显示了超出范围(0
)时的行为。
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