Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何循环列并计算平均值?_Arrays_Vba_Excel_Loops - Fatal编程技术网

Arrays 如何循环列并计算平均值?

Arrays 如何循环列并计算平均值?,arrays,vba,excel,loops,Arrays,Vba,Excel,Loops,我想找出一列中每段10个值的平均值。(见数据系列图片) 一直持续到数据集的底部。数据集的长度可以不同,代码必须是“通用”类型的 基于其他代码段,我已尝试这样做: Sub tenthavg() Dim currentIndex As Long Dim myArray() As Variant Dim rng As Range ReDim myArray(1 To 10) Range("b1", Range("b1").End(xlDown)).Select Set myArray

我想找出一列中每段10个值的平均值。(见数据系列图片) 一直持续到数据集的底部。数据集的长度可以不同,代码必须是“通用”类型的

基于其他代码段,我已尝试这样做:

Sub tenthavg()

Dim currentIndex As Long
Dim myArray() As Variant
Dim rng As Range

ReDim myArray(1 To 10)     

Range("b1", Range("b1").End(xlDown)).Select
Set myArray = Selection        
currentIndex = 1

Do Until currentIndex + 1 > UBound(myArray)
    ActiveSheet.Cells(currentIndex, "T") = AverageOfSubArray(myArray, currentIndex, 10)
    currentIndex = currentIndex + 1
Loop                   

End Sub

'=================================================================

Function AverageOfSubArray(myArray As Variant, startIndex As Long,   elementCount As Long) As Double
Dim runningTotal As Double
Dim i As Long

For i = startIndex To (startIndex + elementCount - 1)
    runningTotal = runningTotal + val(myArray(i))
Next i
AverageOfSubArray = runningTotal / elementCount
End Function
不幸的是,我不能让它工作。我这样做对吗


如果是,我做错了什么

您可以通过更简单的方式获得结果:

Sub tenthavg()

Dim LastRow As Long
LastRow = ThisWorkbook.Sheets("Your Sheet Name").Columns(2).Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Dim myArray(1 To 10) As Double

If LastRow < 10 Then
    MsgBox "There's not enough data!"
Else
    On Error Resume Next
    For x = 1 To LastRow - 9
        For y = 1 To 10
            myArray(y) = ThisWorkbook.Sheets("Your Sheet Name").Cells(y + x - 1, 2).Value
        Next y
        ThisWorkbook.Sheets("Your Sheet Name").Cells(x, 20).FormulaR1C1 = 0
        ThisWorkbook.Sheets("Your Sheet Name").Cells(x, 20).FormulaR1C1 = Application.Average(myArray)
    Next x
End If

End Sub
Sub-tenthavg()
最后一排一样长
LastRow=ThisWorkbook.Sheets(“您的工作表名称”)。列(2)。查找(“*”),搜索顺序:=xlByRows,查找:=xlValues,搜索方向:=xlPrevious)。行
将myArray(1到10)调暗为双精度
如果LastRow<10,则
MsgBox“没有足够的数据!”
其他的
出错时继续下一步
对于x=1到最后一行-9
对于y=1到10
myArray(y)=ThisWorkbook.Sheets(“您的工作表名称”).Cells(y+x-1,2).Value
下一个y
ThisWorkbook.Sheets(“您的工作表名称”).Cells(x,20)。公式1c1=0
ThisWorkbook.Sheets(“您的工作表名称”).Cells(x,20)。FormulaR1C1=Application.Average(myArray)
下一个x
如果结束
端接头

请注意:我假设您的数据从
B1
开始,并且您希望在
T
列上输出,但这并不是一种成功的方法。。。不要选择
Select
ing
EndDown
和从交互式工作中借用的其他概念,而是利用VBA自身的机制

“通用”方法将范围起始地址、批大小和偏移量作为参数放置结果的位置

Sub AvgX(MyR As Range, S As Integer, ORow As Integer, OCol As Integer)
' MyR = start of range
' S   = batch size
' OCol, ORow = Offsets to place result in relation to last batch value
Dim Idx As Integer, Jdx As Integer, RSum As Variant

    Idx = 1
    RSum = 0
    Do
        For Jdx = 1 To S
            RSum = RSum + MyR(Idx, 1)
            Idx = Idx + 1
            If MyR(Idx, 1) = "" Then Exit Do
        Next Jdx
        MyR(Idx - 1, 1).Offset(ORow, OCol) = RSum / (Jdx - 1)
        RSum = 0
    Loop
End Sub
并被称为

Sub Test()
    AvgX [C4], 10, 0, 1
End Sub
给你这个结果


虽然这可能效果不错,但您在工作表中的一个单元格中,B列中的每一个单元格都要访问三次。这可能会增加很快的速度,并使速度大大降低。我刚刚意识到您已经找到/接受了答案。所以,我没有必要更新我的答案。你的帖子内容丰富,是一个很好的资源。不应该被删除我的想法很简单:你喜欢我的一篇文章
-->
你投票(表达你的感激),然后这篇文章就保留了下来。没有投票告诉我我的帖子没有用,它被删除了。简单。只是在一些罕见的情况下,每当我想把我的帖子作为个人的便条时,我还是会把它留下。所以,下次你喜欢这个网站上的帖子时,一定要投赞成票,我很确定这个帖子不会被删除。