添加唯一值并排除常数-优化公式

时间:2018-12-18 20:47:22

标签: excel optimization sumifs

我需要添加唯一值,并确保排除常数(例如10)

Acct #        Value
9xxx123        50
9xxx123        50
9xxx123        10
9xxx123        15
9xxx234        10
9xxx234        25
9xxx234        25
9xxx234        30

答案应该是:9xxx123 = 65,而9xxx234 = 55

以下公式为我提供了正确的答案:

= SUMPRODUCT(((($ A $ 2:$ A $ 9 = E2)* $ B $ 2:$ B $ 9)/(COUNTIFS($ A $ 2:$ A $ 9,E2,$ B $ 2:$ B $ 9,$ B $ 2:$ B $ 9)+($ A $ 2:$ A $ 9 <> E2))))

但是,由于我要分析的数据量很大,因此我的计算机需要将近一个小时才能执行所有计算。关于如何优化此公式的任何建议?

谢谢!

狮子座

1 个答案:

答案 0 :(得分:0)

用户定义的函数如何?

Dim cell As Range
Dim mysum As Long, i As Long, j As Long
Dim checkarr As Variant
Function SUMOMITTINGVALUE(accountrange As Range, accountnumber As Range, omitvalue As Long) As Long

    mysum = 0
    j = 0
    ReDim checkarr(0 To 0)

    For Each cell In accountrange
        If cell.Value = accountnumber And _
           cell.Offset(, 1).Value <> omitvalue Then
            For i = 0 To UBound(checkarr)
                If checkarr(i) = cell.Offset(, 1).Value Then
                    Exit For
                ElseIf i = UBound(checkarr) Then
                    mysum = mysum + cell.Offset(, 1).Value
                    checkarr(j) = cell.Offset(, 1).Value
                    j = j + 1
                    ReDim Preserve checkarr(0 To j)
                End If
            Next i
        End If
    Next cell

    SUMOMITTINGVALUE = mysum
End Function

img1