相应列值的最小值

时间:2014-08-03 12:36:55

标签: excel excel-formula libreoffice libreoffice-calc

我有两列数字。我想在一个单独的单元格中计算一笔金额。总和将包括这两列中相应单元格的最小值。

示例:

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

我需要一个公式,在单元格中计算1 + 3 + 0 + 5的总和

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

这可能在一个公式中(独立于#rows)吗?

目前使用LibreOffice Calc,但Excel解决方案非常受欢迎。

2 个答案:

答案 0 :(得分:4)

您可以使用数组公式Libre Office的文档和Excel的文档):

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

Ctrl + Shift + Enter确认,而不是简单回答。

答案 1 :(得分:1)

嗯,你可以用公式做到这一点,但它的可扩展性不高。例如,您可以这样做:

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

将适用于您描述的情况。但是,我很欣赏,如果你有大量的行,那么这不会很好地扩展。在这种情况下,我认为您需要一个VBA宏,因为我无法看到这样做的方法。我准备好通过一些Excel公式大师来纠正。

对于VBA解决方案,您可以尝试以下(在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

设置为TRUE时,可选参数计算最小值,而不是默认情况下此宏计算的最大值。在您的示例中,您需要像这样调用它:

=sumOfMax(A1:B4, TRUE)

请记住,您需要将代码放在标准代码模块中。

相关问题