nnsea 发表于 2023-10-25 15:52:02

求平均数VBA


Sub 数组汇总() '求平均分
arr =
For Each num In arr
    n = n + 1
    lj = lj + num
Next
    MsgBox "平均分为:" & lj / n
End Sub

Sub 动态区域数组汇总() '求平均分
arr = Range(, Cells(Rows.Count, 2).End(xlUp))
For Each num In arr
    n = n + 1
    lj = lj + num
Next
    MsgBox "平均分为:" & lj / n
End Sub

Sub 有条件的汇总() '大于等于80的平均分
arr = Range(, Cells(Rows.Count, 2).End(xlUp))
For Each num In arr
    If num >= 80 Then
    n = n + 1
    lj = lj + num
    End If
Next
    MsgBox "平均分为:" & lj / n
End Sub
'结论:如果对数组中元素进行循环,是从上到下,从左到右
页: [1]
查看完整版本: 求平均数VBA