示例:
A1 1
A2 2
A3 2
A4 3
A5 3
A6 3
A7 4
A8 4
A9 4
A10 4
sum = 1+2+3+4 = 10
我知道它可能涉及excel功能,如SUM,IF,FREQUENCY,MATCH,ROW等。但我不知道如何将它们放在一起,无论我尝试什么都会有错误。请帮忙。谢谢。
答案 0 :(得分:0)
这是我之前为自己写的一个UDF。它将在任何2D范围内添加唯一值。
Public Function SUMU(ByVal r As Range) As Double
On Error Resume Next
Application.Volatile
Dim nUniques() As Double
Dim v As Variant, vValues As Variant
Dim i As Long, nCount As Long
Dim bMatch As Boolean
vValues = r.Value
If r.Count = 1 Then
SUMU = r.Value
Exit Function
End If
nCount = 1
ReDim nUniques(1 To 1)
For Each v In vValues
If v <> "" And IsNumeric(v) Then
bMatch = False
For i = LBound(nUniques) To UBound(nUniques)
If v = nUniques(i) Then
bMatch = True
End If
Next i
If bMatch = False Then
nUniques(nCount) = v
nCount = nCount + 1
ReDim Preserve nUniques(1 To nCount)
End If
End If
Next
SUMU = Application.WorksheetFunction.Sum(nUniques)
End Function