数组的累积总和

时间:2017-09-21 12:20:34

标签: arrays excel vba excel-vba cumulative-sum

我有一个整数数组值,想要找到一种计算累积和(follow_up)的简单方法。

我已经创建了这个功能:

S = Data(1) + Data(2) + ... + Data(x)

它工作正常。但是,我想知道是否有更好的方法(主要是不使用任何额外的功能,基本上只使用像Function CumulativeSum(Data() As Integer, k As Integer) As Integer For entry = 1 To k CumulativeSum = CumulativeSum + Data(entry) Next entry End Function 这样的excel函数)。我在网上进行了一次小搜索,但在此基础上找不到任何东西。

我知道我不是要求更正任何代码,我只是要求一个不是本论坛真正目的的替代方案。但是,我也怀疑答案可能很简单,所以...如果有人关心帮助我,我会非常非常感激!如果您找到类似问题的答案,请与我分享链接,我将删除此链接。

我很抱歉可能缺乏明确的要求:我只想找到一种简单的方法,使用宏例程本身的简单函数计算累积和,不使用Application.Sum函数我创建或由用户创建的任何其他功能。

4 个答案:

答案 0 :(得分:9)

如果你想从Array(a,b,c)实现像Array(a,a + b,a + b + c)这样的累积数组数组,那么这就是函数实现它,如果你想传递开始和结束参数:

Public Sub TestMe()

    Dim outputArray     As Variant
    Dim inputArray      As Variant
    Dim counter         As Long

    inputArray = Array(1, 2, 4, 8, 16, 32, 64)

    outputArray = generateCumulativeArray(inputArray, 1, 4)
    For counter = LBound(outputArray) To UBound(outputArray)
        Debug.Print outputArray(counter)
    Next counter

    outputArray = generateCumulativeArray(inputArray, toValue:=4)
    For counter = LBound(outputArray) To UBound(outputArray)
        Debug.Print outputArray(counter)
    Next counter

End Sub

Public Function generateCumulativeArray(dataInput As Variant, _
                                        Optional fromValue As Long = 0, _
                                        Optional toValue As Long = 0) As Variant

    Dim i                   As Long
    Dim dataReturn          As Variant
    ReDim dataReturn(0)
    dataReturn(0) = dataInput(fromValue)

    For i = 1 To toValue - fromValue
        ReDim Preserve dataReturn(i)
        dataReturn(i) = dataReturn(i - 1) + dataInput(fromValue + i)
    Next i
    generateCumulativeArray = dataReturn
End Function

关于只是汇总一个数组,这是这样做的方法: 您可以使用WorksheetFunction.并将数组作为参数传递。因此,您可以获得所有功能,例如AverageMinMax等:

Option Explicit

Public Sub TestMe()

    Dim k As Variant
    k = Array(2, 10, 200)
    Debug.Print WorksheetFunction.Sum(k)
    Debug.Print WorksheetFunction.Average(k)

End Sub

如果你想要从给定的开始到给定的结尾的总和,最简单的方法可能是创建一个新数组并将其完全求和。在Python中,这称为切片,在VBA中,这可以手动完成:

Public Sub TestMe()

    Dim varArr          As Variant
    Dim colSample       As New Collection

    varArr = Array(1, 2, 4, 8, 16, 32, 64)

    colSample.Add (1)
    colSample.Add (2)
    colSample.Add (4)
    colSample.Add (8)

    Debug.Print WorksheetFunction.Sum(generateArray(varArr, 2, 4))
    Debug.Print WorksheetFunction.Sum(generateArray(colSample, 2, 4))

End Sub

Public Function generateArray(data As Variant, _
                              fromValue As Long, _
                              toValue As Long) As Variant

    Dim i                   As Long
    Dim dataInternal        As Variant
    Dim size                As Long

    size = toValue - fromValue
    ReDim dataInternal(size)

    For i = LBound(dataInternal) To UBound(dataInternal)
        dataInternal(i) = data(i + fromValue)
    Next i

    generateArray = dataInternal

End Function

这个想法是generateArray函数返回一个新数组。因此,它的完整总和就是你所需要的。它也适用于集合,不仅适用于数组。要小心,在使用集合时,它们从索引1开始,而数组(通常)从0开始。如果要对数组和集合使用相同的索引,则将generateArray函数更改为此函数:

Public Function generateArray(data As Variant, _
                              fromValue As Long, _
                              toValue As Long) As Variant

    Dim i                   As Long
    Dim dataInternal        As Variant
    Dim size                As Long

    size = toValue - fromValue
    ReDim dataInternal(size)

    If IsArray(data) Then
        For i = LBound(dataInternal) To UBound(dataInternal)
            dataInternal(i) = data(i + fromValue)
        Next i
    Else
        For i = LBound(dataInternal) To UBound(dataInternal)
            dataInternal(i) = data(i + fromValue + 1)
        Next i
    End If

    generateArray = dataInternal

End Function

或者在顶部写Option Base 1,数组将从1开始(不建议!)。

答案 1 :(得分:2)

对于累计金额,请尝试以下

Function CumulativeSum(Data() As Integer, k As Integer) As Integer
    Dim tempArr
    tempArr = Data
    ReDim Preserve temp(0 To k - 1)
    CumulativeSum = WorksheetFunction.Sum(tempArr)
End Function

编辑:

Sub Demo()
    Dim MyArray
    Dim i As Long
    MyArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

    Debug.Print MyArray(LBound(MyArray))
    For i = LBound(MyArray) + 1 To UBound(MyArray)
        MyArray(i) = MyArray(i - 1) + MyArray(i)
        Debug.Print MyArray(i)
    Next i
End Sub

上面的代码更新来自的数组arr 1, 2, 3, 4, 5, 6, 7, 8, 9

1, 3, 6, 10, 15, 21, 28, 36, 45

答案 2 :(得分:2)

试试这个:

Sub test()
Dim arr As Variant
arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Dim mySum As Long, k As Long
Dim wsf As WorksheetFunction
Set wsf = Application.WorksheetFunction

k = 6
'operative line below
mySum = wsf.Sum(wsf.Index(arr, 1, Evaluate("ROW(1:" & k & ")")))

MsgBox mySum

End Sub

答案 3 :(得分:0)

此函数返回一个数组,其中包含原始向量的累加和。

<something>Value<something>