0

我想要一个计算给定范围之和的公式。

说我1,5在 cell 中A1。我想要一个公式来计算1+2+3+4+5。也许以更高级的方式,我想输入1,5;6,3;1,4;...并让它计算(1+2+3+4+5)+(6+5+4+3)+(1+2+3+4)

不会有负数。

这在Excel中可能吗?对于 range 6,3,获得较小的数字并数到 6 可能是最容易的。

4

3 回答 3

3

这是一个用户定义的函数,可以满足您的需求。它也适用于底片。没有错误检查,例如,对于单元格中的字母,分号之间有多个逗号(第一个逗号无论如何都会产生#VALUE 错误)。

在工作簿的模块或插件中输入此函数:

Function AddSubstringProgressions(CellContent As String)
Dim Progressions As Variant
Dim Progression As Variant
Dim i As Long, j As Long
Dim Total As Long
Dim Stepwise As Long

Progressions = Split(CellContent, ";")
For i = LBound(Progressions) To UBound(Progressions)
    Progression = Split(Progressions(i), ",")
    If Val(Progression(LBound(Progression))) > Val(Progression(UBound(Progression))) Then
        Stepwise = -1
    Else
        Stepwise = 1
    End If
    For j = Progression(LBound(Progression)) To Progression(UBound(Progression)) Step Stepwise
       Total = Total + j
    Next j
Next i

AddSubstringProgressions = Total
End Function

像这样称呼它:

在此处输入图像描述

于 2012-07-21T15:51:52.093 回答
1

对于工作表函数方法,您可以尝试以下方法之一:

  1. 基本情况,例如 A1 =1,56,3

    =SUMPRODUCT(ROW(INDIRECT(SUBSTITUTE(A1,",",":"))))
    
  2. 一般情况,例如 A1 = 1,5;6,3;1,4

    定义名称A =EVALUATE("{"&A1&"}"),然后输入:

    =SUMPRODUCT(ABS(MMULT(A^2,{1;-1}))/2+A)/2
    

更新。基于上述方法的简短VBA udf ...

Function SumSeries(Val As String)
SumSeries = Evaluate("SUM(ABS(MMULT({" & Val & "}^2,{1;-1}))/2+{" & Val & "})/2") 
End Function
于 2012-07-21T16:51:23.110 回答
0

这是我想出的:

Sub addRange()
    Dim cell As Range
    Dim count As Integer
    count = Application.WorksheetFunction.CountA(Range("A:A"))
    Dim i As Integer
    i = 1
    Do While i <= count
        Dim low As Integer
        Dim high As Integer
        low = Mid(Range("A" & i), 1, Application.WorksheetFunction.Search(",", Range("A" & i)) - 1)
        high = Mid(Range("A" & i), Application.WorksheetFunction.Search(",", Range("A" & i)) + 1, Len(Range("A" & i)))
        If (low > high) Then
            Dim copy As Integer
            copy = low
            low = high
            high = copy
        End If
        Range("B" & i) = adder(low, high)
        i = i + 1
    Loop
End Sub

Function adder(low As Integer, high As Integer) As Integer
    Dim sum As Integer
    sum = 0
    Do While low <= high
        sum = sum + low
        low = low + 1
    Loop
    adder = sum
End Function

这假设您在单元格 A1:An 中具有2,56,4、等值。1,8它不适用于2,5;6,4您所描述的更高级功能的值,但它可以完成基本功能的工作。

于 2012-07-21T15:58:21.640 回答