Question

If there a way to use a sumif combined with a countif?

for example if I have

TransactionNo|NumberToSum
    1        |  
    1        |  9
    2        |  6
    3        |  3
    4        |  
    4        |  4
    5        |  6
    6        |  4
    7        |  
    7        |  9
    8        |  7
    9        |  4
    10       |  
    10       |  1
    11       |  3
    12       |  6

My result would be: 23

because the values 1,4,7,10 are duplicated, so I need to sum the NumberToSum of those 9,4,9,1 in this case.

Ideally I would like somethings along the lines of =SumIf(A:A,Count>1,B:B)

Where B is summed if the count of A inside of A:A is > 1.

but =SUMIF(A:A,COUNT(A:A)>1,B:B) does not work.

NOTE: the Data is NOT sorted as it appears in my example.

If I wanted to do it in VBA one quick way could be:

Sub sumdups()
Dim rng As Range
Dim rng2 As Range
Dim Sum As Long

Set rng = [A2:A17]

For Each rng2 In rng
    If WorksheetFunction.CountIf(Range("A1:A17"), rng2.Value) > 1 Then
        Sum = Sum + rng2.Offset(0, 1).Value
    End If
Next rng2
End Sub

Another option I have tried is to add a column with the formula

 =IF(COUNTIF(A:A,A2)>1,B2,"")

Then sum the helper column, but I am hoping there is a simple formula that can do the same all in one cell.

I am really just looking for a faster formula that can be entered in a single cell, without the need for any additional calculations.

Was it helpful?

Solution

This formula works for me:

=SUMPRODUCT(1*(COUNTIF(A1:A100,A1:A100)>1),(B1:B100))

or for entire column (a little bit slower):

=SUMPRODUCT(1*(COUNTIF(A:A,A:A)>1),(B:B))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top