1:求取A1:C15的平均值,虽然Excel命令可以多次平均直接解决,但本问题提供一种思路VBA来进行编程计算
2:代码
Private Sub Workbook_Open()
For Each c In ThisWorkbook.Sheets("Sheet1").Range("A1:C15")
TotalValue = TotalValue + c.Value
Next
AverageValue = TotalValue / (ThisWorkbook.Sheets("Sheet1").Range("A1:C15").Rows.Count * ThisWorkbook.Sheets("Sheet1").Range("A1:C15").Columns.Count)
MsgBox AverageValue
End Sub
3:结果
Discussion:有助于对ExcelVBA编程的理解和计算。