Sub TestResult()
Dim score As Integer
Dim over80 As Integer
Dim i As Integer
Dim total As Long
Dim average As Double
Dim maxScore As Integer
Dim maxRow As Integer
total = 0
over80 = 0
maxScore = 0
maxRow = 0
For i = 2 To 21
score = Range("A" & i).Value
total = total + score
If score > maxScore Then
maxScore = score
maxRow = i
End If
If score >= 80 Then
over80 = over80 + 1
End If
Next i
average = total / 20
Range("B2").Value = total
Range("B3").Value = average
Range("B4").Value = over80
Range("B5").Value = maxScore
Range("B6").Value = maxRow
End Sub
今日のポイント
① 変数に一度入れてから使う
② 「値」と「その値があった場所」は別の変数にする
③ 初期値を設定する習慣
つまり、
① Forで1行ずつ見る
→ ② セルの値を変数に入れる
→ ③ Ifで条件判定する
→ ④ 合計や件数を更新する
コメント