我想计算一列中的第一个60元素,第二个60元素等的总和。我到目前为止写的代码不起作用:
Function sixtysum(data)
Dim xa()
For i = 1 To 5475 Step 60
For j = i To i + 60
xa(j) = 0
xa(j) = xa(j) + data(j)
Next j
Next i
sixtysum = xa()
End Function
提前致谢!
答案 0 :(得分:1)
使用函数将内存中的数组返回给其他函数/子程序。的未测试强>
Function f_sixtysum(data as Range) As Variant
Dim i as Long, x as Integer
Dim startRng as Range, endRng as Range
Dim mySum as Double
Dim var as Variant
If data Is Nothing then GoTo EarlyExit
'## Make sure your array is properly sized, this is a one-dimensional array with n/60 elements.
ReDim var(Application.WorksheetFunction.RoundUp((data.Rows.Count/60) - 1,0)
'## Make sure the range we're working with only the FIRST column
Set data = data.Resize(data.Rows.Count, 1)
'## Iterate over the range in increment of 60
For i = 1 to data.Cells.Count step 60
'## Compute the sum of 60 cells and store in the temporary array "var"
var(x) = Application.WorksheetFunction.Sum( _
Range(data(i), Range(data(i+59)))
'## increment our array counter variable
x = x + 1
Next
'## Assign our temporary array "var" to the function's return value
f_sixtysum = var
Exit Function
EarlyExit:
'Returns an empty array
f_sixtysum = Array()
End Sub