以下是VBA函数,用于计算唯一值计数(贷记为SO: Count unique values in Excel) 是否可以添加标准参数?就像在功能“ countifs”中一样?
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
答案 0 :(得分:2)
这是我创建的:
代码如下:
Public Function CountUnique(inputRange As Range, paramRange As Range) As Long
Dim dict As New Dictionary
Dim cellInput As Range
Dim cellParam As Range
Dim keepLooking As Boolean
For Each cellParam In paramRange
keepLooking = True
For Each cellInput In inputRange
If cellParam.Value2 = cellInput.Value2 And keepLooking Then
If Not dict.Exists(cellInput.Value2) Then
dict.Add cellInput, 0
keepLooking = False
End If
End If
Next
Next
CountUnique = dict.Count
End Function