数组的累计和

我有一个整数值的数组,并希望find一个简单的方法来计算其累积和( S = Data(1) + Data(2) + ... + Data(x) )。

我已经创build了这个function:

 Function CumulativeSum(Data() As Integer, k As Integer) As Integer For entry = 1 To k CumulativeSum = CumulativeSum + Data(entry) Next entry End Function 

它工作正常。 但是,我不知道是否有更好的方法(主要是没有使用任何额外的function,并且基本上只使用像Application.Sum这样的Excel函数)。 我在网上做了一个小search,但没有在这个基础上find任何东西。

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

我非常抱歉,可能是我缺less明确的需求:我只是想find一个简单的方法来计算累积和,使用macros函数本身的简单函数,而不使用我创build的CumulativeSum函数或任何其他函数用户。

尝试这个:

 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 

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

 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. 你可以传递数组作为参数。 因此,您可以获得所有的function,例如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 

如果你想从一个给定的开始到一个给定的结束 ,最简单的方法可能是创build一个新的数组并将其总和。 在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开始(不build议!)。

累计总和请尝试以下内容

 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