首页 > 解决方案 > 一个数字数组的总和,在 1 个单元格中用逗号分隔,仅当至少有 5 个元素时

问题描述

  1. 我需要同一单元格中元素数组的总和。
  2. 它们用逗号分隔。
  3. 如果有 <5 个元素,则不要计算该单元格的总和。

例子:

cell 1A: 2,1,2,3,1  cell 1B: 9 (the sum)

cell 2A: 1, 2       cell 2B: -- (nothing as there are less than five elements).

我试图解释自己。希望很清楚。

标签: excel-formula

解决方案


我在这里问了一个类似的问题:

老问题

但是你有一个额外的要求,所以试试下面的用户定义函数:

Public Function SumWithin(s As String) As Variant
    If s = "" Then
        SumWithin = ""
        Exit Function
    End If

    If InStr(1, s, ",") = 0 Then
        SumWithin = ""
        Exit Function
    End If

    arry = Split(s, ",")
    If UBound(arry) < 4 Then
        SumWithin = ""
        Exit Function
    End If

    For Each a In arry
        SumWithin = SumWithin + CDbl(a)
    Next a
End Function

一些例子:

在此处输入图像描述

这种方法避免了典型的Text-to-Columns方法所需的所有额外单元格。没有 VBA也可以做到这一点,但它涉及到数组公式


推荐阅读