=AVERAGE(E:E)
如果非要用VBA,也可以调用表函数:
平均值= = Application.Average(Range("E:E"))
下面的自定义函数就可以计算所选中列的算术平均值:Function mySUM()
On Error Resume Next
c = Selection.Cells(1, 1).Column
For Each r In Selection.Rows
mySUM = mySUM + Cells(r.Row, c)
Next
mySUM = mySUM / Selection.Rows.Count
ri = Selection.Cells(1, 1).Row + Selection.Rows.Count - 1
Cells(ri + 1, c) = mySUM
End Function
如果确定了列,那就可以这样:
Function mySUM()
On Error Resume Next
c = 2
For Each r In Selection.Rows
mySUM = mySUM + Cells(r.Row, c)
Next
mySUM = mySUM / Selection.Rows.Count
ri = Selection.Cells(1, 1).Row + Selection.Rows.Count - 1
Cells(ri + 1, c) = mySUM
End Function
还可以把c当作参数传递:
Function mySUM(c As Integer)
On Error Resume Next
For Each r In Selection.Rows
mySUM = mySUM + Cells(r.Row, c)
Next
mySUM = mySUM / Selection.Rows.Count
ri = Selection.Cells(1, 1).Row + Selection.Rows.Count - 1
Cells(ri + 1, c) = mySUM
End Function
如果一定要用平均函数的话,可以使用WorksheetFunction.Average(arr(i, 1), arr(i, 2), arr(i, 3), arr(i, 4), arr(i, 5), arr(i, 6), arr(i, 7), arr(i, 8), arr(i, 9), arr(i, 10))
Sub test10()Dim arr(1 To 13, 1 To 11) As Double
Dim i As Integer, j As Integer
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2) - 1
arr(i, j) = i + j
Cells(i, j) = arr(i, j)'可视化验证
Next j
arr(i, 11) = WorksheetFunction.Average(arr(i, 1), arr(i, 2), arr(i, 3), arr(i, 4), arr(i, 5), arr(i, 6), arr(i, 7), arr(i, 8), arr(i, 9), arr(i, 10))
Cells(i, j) = arr(i, 11)'可视化验证
Next i
End Sub
其实如果仅仅想得到结果,采用qq2298511336就很好。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)