第6講 同じことの横縦の繰り返し(2次元ループ)
第8話 同じことの横縦の繰り返しによる成績一覧表マクロ完成

2013
解答例
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) = "評価"
  Cells(6, 11) = "3段階評価"
  Cells(6, 12) = "4段階評価"
  Cells(6, 13) = "5段階評価"
  Dim i As Byte, j As Byte, w As Integer
  For i = 0 To 9
    Cells(7 + i, 2) = i + 1
    For j = 0 To 4
      Cells(7 + i, 3 + j) = Int(100 * Rnd())
    Next
  Next
  For i = 0 To 4
    w = 0
    For j = 0 To 9
      w = w + Cells(7 + j, 3 + i)
    Next
    Cells(17, 3 + i) = w
    Cells(18, 3 + i) = w / 10
    If w / 10 >= 50 Then
      Cells(19, 3 + i) = "合格"
    Else
      Cells(19, 3 + i) = "不合格"
    End If
  Next
  For i = 0 To 9
    w = 0
    For j = 0 To 4
      w = w + Cells(7 + i, 3 + j)
    Next
    Cells(7 + i, 8) = w
    Cells(7 + i, 9) = w / 5
    If w / 5 >= 50 Then
      Cells(7 + i, 10) = "合格"
    Else
      Cells(7 + i, 10) = "不合格"
    End If
    If w / 5 >= 55 Then
      Cells(7 + i, 11) = "優秀"
    Else
      If w / 5 >= 50 Then
        Cells(7 + i, 11) = "普通"
      Else
        Cells(7 + i, 11) = "努力が必要"
      End If
    End If
    If w / 5 >= 60 Then
      Cells(7 + i, 12) = "天才級"
    Else
      If w / 5 >= 55 Then
        Cells(7 + i, 12) = "秀才級"
      Else
        If w / 5 >= 50 Then
          Cells(7 + i, 12) = "平凡"
        Else
          Cells(7 + i, 12) = "不出来"
        End If
      End If
    End If
    If w / 5 >= 65 Then
      Cells(7 + i, 13) = "神"
    Else
      If w / 5 >= 60 Then
        Cells(7 + i, 13) = "准超越者"
      Else
        If w / 5 >= 55 Then
          Cells(7 + i, 13) = "人間"
        Else
          If w / 5 >= 45 Then
            Cells(7 + i, 13) = "人造人間ベム・ベロ・ベラ級"
          Else
            Cells(7 + i, 13) = "ピノキオ以下の存在"
          End If
        End If
      End If
    End If
  Next
  
  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

今回は、皆さんに慣れていただくために、
For i = 0 To 9 としました。
  For i = 0 To 9
    Cells(7 + i, 2) = i + 1
    For j = 0 To 4
      Cells(7 + i, 3 + j) = Int(100 * Rnd())
    Next
  Next
の9を39などと変更すれば
vba
40人のデータも可能になります。

第7話へ 第7講第1話へ

トップ

vc++講義へ

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