第5講 もしもボックス(If文)
第4話 成績一覧表に平均を加える



解答例
Private Sub CommandButton1_Click()
  Cells(6, 2) = "出席番号"
  Cells(6, 3) = "国語"
  Cells(6, 4) = "社会"
  Cells(6, 5) = "数学"
  Cells(6, 6) = "理科"
  Cells(6, 7) = "英語"
  Cells(6, 8) = "合計"
  Cells(6, 9) = "平均"
  Cells(6, 10) = "評価"
  Dim i As Byte, w As Integer
  For i = 1 To 10
    Cells(6 + i, 2) = i
    Cells(6 + i, 3) = Int(100 * Rnd())
    Cells(6 + i, 4) = Int(100 * Rnd())
    Cells(6 + i, 5) = Int(100 * Rnd())
    Cells(6 + i, 6) = Int(100 * Rnd())
    Cells(6 + i, 7) = Int(100 * Rnd())
  Next
  w = 0
  For i = 1 To 10
    w = w + Cells(6 + i, 3)
  Next
  Cells(17, 3) = w
  Cells(18, 3) = w / 10
  w = 0
  For i = 1 To 10
    w = w + Cells(6 + i, 4)
  Next
  Cells(17, 4) = w
  Cells(18, 4) = w / 10
  w = 0
  For i = 1 To 10
    w = w + Cells(6 + i, 5)
  Next
  Cells(17, 5) = w
  Cells(18, 5) = w / 10
  w = 0
  For i = 1 To 10
    w = w + Cells(6 + i, 6)
  Next
  Cells(17, 6) = w
  Cells(18, 6) = w / 10
  w = 0
  For i = 1 To 10
    w = w + Cells(6 + i, 7)
  Next
  Cells(17, 7) = w
  Cells(18, 7) = w / 10
  w = 0
  For i = 1 To 5
    w = w + Cells(7, 2 + i)
  Next
  Cells(7, 8) = w
  Cells(7, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(8, 2 + i)
  Next
  Cells(8, 8) = w
  Cells(8, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(9, 2 + i)
  Next
  Cells(9, 8) = w
  Cells(9, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(10, 2 + i)
  Next
  Cells(10, 8) = w
  Cells(10, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(11, 2 + i)
  Next
  Cells(11, 8) = w
  Cells(11, 9) = w / 5
  w = 0
  For i = 1 To 5
    w = w + Cells(12, 2 + i)
  Next
  Cells(12, 8) = w
  Cells(12, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(13, 2 + i)
  Next
  Cells(13, 8) = w
  Cells(13, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(14, 2 + i)
  Next
  Cells(14, 8) = w
  Cells(14, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(15, 2 + i)
  Next
  Cells(15, 8) = w
  Cells(15, 9) = w / 5
  w = 0
   For i = 1 To 5
    w = w + Cells(16, 2 + i)
  Next
  Cells(16, 8) = w
  Cells(16, 9) = w / 5
  w = 0
  For i = 1 To 10
    w = w + Cells(6 + i, 8)
  Next
  Cells(17, 8) = w
  Cells(18, 8) = w / 10
  Dim v As Single
  v = 0
  For i = 1 To 10
    v = v + Cells(6 + i, 9)
  Next
  Cells(17, 9) = v
  Cells(18, 9) = v / 10
  Cells(17, 2) = "合計"
  Cells(18, 2) = "平均"
  Cells(19, 2) = "評価"
End Sub

さて、皆さんもしもボックスを使い評価を入れましょう。
生徒の平均点が50点以上なら合格、50点未満なら不合格と表示させるにはどうしたらよいですか。
各教科の平均点についても同様とします。
(データは毎回異なります。)


第3話へ 第5話へ

トップ

vc++講義へ

初心者のための世界で一番わかりやすいVisual C++入門基礎講座
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ
vb講義へ
VB講義基礎へ
初心者のためのJava 入門 基礎から応用まで
数学研究室に戻る