Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum minimum of corresponding column values

I have two columns with numbers. I would like to calculate in a separate cell a sum. The sum will include the minimums of corresponding cells in these two columns.

Example:

        A  |  B
       --------
[1]     1  |  2
[2]     4  |  3
[3]     0  |  1
[4]     5  |  5

I need a formula that would calculate in a cell the sum of 1 + 3 + 0 + 5 where

* 1 is the MIN(A1,B1), 
* 3 is the MIN(A2,B2) 
* 0 is the MIN(A3,B3)
* 5 is the MIN(A4,B4)

Is that possible in a single formula (independent of #rows)?

Working with LibreOffice Calc at the moment, but Excel solutions more than welcome.

like image 774
Thalis K. Avatar asked Aug 03 '14 12:08

Thalis K.


People also ask

How do you sum corresponding values in Excel?

If you want, you can apply the criteria to one range and sum the corresponding values in a different range. For example, the formula =SUMIF(B2:B5, "John", C2:C5) sums only the values in the range C2:C5, where the corresponding cells in the range B2:B5 equal "John."

How do you sum a column in Excel based on another column?

(1) Select the column name that you will sum based on, and then click the Primary Key button; (2) Select the column name that you will sum, and then click the Calculate > Sum. (3) Click the Ok button.


2 Answers

You can use an array formula (doc for Libre Office and for Excel) for this:

=SUM(IF(A1:A4<B1:B4, A1:A4, B1:B4))

Confirm by Ctrl+Shift+Enter instead of simple Enter.

like image 173
Aprillion Avatar answered Oct 04 '22 15:10

Aprillion


Well, you can do this with a formula but it's not very scalable. For example, you can do this:

=SUM(MIN(A1:B1),MIN(A2:B2),MIN(A3:B3), MIN(A4:B4))

that will work in the case you described. However, I appreciate that if you have a large number of rows then this won't scale nicely. In that case, I think you'll need a VBA macro as I can't see a way to do this. I'm prepared to be corrected though by some Excel formula guru.

For a VBA solution, you can try the following (found in the OzGrid forums):

Function sumOfMax(ByVal inputRange As Range, Optional doMin As Boolean) As Double
    Dim inRRay As Variant
    Dim currentMax As Double
    Dim i As Long, j As Long

    Set inputRange = Application.Intersect(inputRange, inputRange.Parent.UsedRange)
    If Nothing Is inputRange Then Exit Function
    inRRay = inputRange.Value
    For i = 1 To UBound(inRRay, 1)
        currentMax = Val(inRRay(i, 1))
        For j = 1 To UBound(inRRay, 2)
            If (Val(inRRay(i, j)) > currentMax) Xor doMin Then currentMax = Val(inRRay(i, j))
        Next j
        sumOfMax = sumOfMax + currentMax
    Next i
End Function

When set to TRUE, the optional parameter calculates the minimums as opposed to the maximums that this macro calculates by default. In your example, you would need to call it like this:

=sumOfMax(A1:B4, TRUE)

Just remember you need to place the code in a standard code module.

like image 34
djikay Avatar answered Oct 04 '22 15:10

djikay