【问题标题】:Sum minimum of corresponding column values对应列值的最小值求和
【发布时间】:2014-08-03 12:36:55
【问题描述】:

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

例子:

        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 解决方案更受欢迎。

【问题讨论】:

    标签: excel excel-formula libreoffice libreoffice-calc


    【解决方案1】:

    您可以为此使用数组公式Libre OfficeExcel 的文档):

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

    通过Ctrl+Shift+Enter而不是简单的回车确认。

    【讨论】:

    • 工作就像一个魅力。谢谢。
    【解决方案2】:

    嗯,您可以使用公式来做到这一点,但它的可扩展性不是很好。例如,您可以这样做:

    =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)
    

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

    【讨论】:

    • LibreOffice 支持 VBA 吗?
    • @Aprillion:不,我不这么认为,但他确实在他的问题中说:“Excel 解决方案非常受欢迎”
    猜你喜欢
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多