代码如下:
Sub scores()
Cells(3, 6) = Cells(3, 3) + Cells(3, 4) + Cells(3, 5)
Cells(3, 7) = Cells(3, 6) / 3
Cells(4, 6) = Cells(4, 3) + Cells(4, 4) + Cells(4, 5)
Cells(4, 7) = Cells(4, 6) / 3
Cells(5, 6) = Cells(5, 3) + Cells(5, 4) + Cells(5, 5)
Cells(5, 7) = Cells(5, 6) / 3
End Sub
也可以稍微简化一下代码,增加一个表示行号的变量i,得到如下代码:
Sub scores()
Dim I
I=3
Cells(i, 6) = Cells(i, 3) + Cells(i, 4) + Cells(i, 5)
Cells(i, 7) = Cells(i, 6) / 3
I=4
Cells(i, 6) = Cells(i, 3) + Cells(i, 4) + Cells(i, 5)
Cells(i, 7) = Cells(i, 6) / 3
I=5
Cells(i, 6) = Cells(i, 3) + Cells(i, 4) + Cells(i, 5)
Cells(i, 7) = Cells(i, 6) / 3
End Sub
即便如此,代码仍旧很繁琐,所以可以用For...Next循环写成:
Sub Scores()
Dim I
For i=3 To 5
Cells(i, 6) = Cells(i, 3) + Cells(i, 4) + Cells(i, 5)
Cells(i, 7) = Cells(i, 6) / 3
Next I
End Sub
代码运行后结果: