第6講 Functionプロシージャ

第6話 基本構造が同一のFunctionプロシージャを1つにまとめる

Funcitonプロシージャ成績一覧表マクロの改良例その2(Functionプロシージャが3つの場合)
Private Sub CommandButton1_Click()

  f1 '生徒の合計点・平均点の計算
  f2 '各教科等の合計点・平均点の計算
  f3 '各生徒の合否判定
  f4 '各生徒の講評
  f5 '各生徒の最高点の算出
  f6 '各生徒の最低点の算
  f7 '各教科と合計・平均の最高点の算出
  f8 '各教科と合計・平均の最低点の算出

End Sub


Function g1(t As Byte, a As Byte)

  Dim i As Integer

  g1 = 0
  If t = 0 Then
    For i = 1 To 5
      g1 = g1 + Cells(6 + a, 1 + i)
    Next
  End If
  If t = 1 Then
    For i = 1 To 40
      g1 = g1 + Cells(6 + i, 1 + a)
    Next
  End If

End Function


Sub f1()

  Dim i As Byte

  '生徒の合計点・平均点の計算
  For i = 1 To 40
    Cells(6 + i, 7) = g1(0, i)
    Cells(6 + i, 8) = g1(0, i) / 5
  Next

End Sub


Sub f2()

  Dim i As Byte

  '各教科等の合計点・平均点の計算
  For i = 1 To 7
    Cells(47, 1 + i) = g1(1, i)
    Cells(48, 1 + i) = g1(1, i) / 40
  Next

End Sub


Sub f3()

  Dim i As Byte

  '各生徒の合否判定
  For i = 1 To 40
    If Cells(6 + i, 7) >= 300 Then
      Cells(6 + i, 11) = "合格"
    End If
    If Cells(6 + i, 7) < 300 Then
      Cells(6 + i, 11) = "不合格"
    End If
  Next

End Sub


Sub f4()

  Dim i As Byte

  '各生徒の講評
  For i = 1 To 40
    If Cells(6 + i, 7) >= 350 Then
      Cells(6 + i, 12) = "あなたはかなり優秀です。"
    End If
    If Cells(6 + i, 7) < 350 And Cells(6 + i, 7) >= 300 Then
      Cells(6 + i, 12) = "おめでとう。"
    End If
    If Cells(6 + i, 7) < 300 And Cells(6 + i, 7) >= 200 Then
      Cells(6 + i, 12) = "合格まで後一歩です。"
    End If
    If Cells(6 + i, 7) < 200 Then
      Cells(6 + i, 12) = "かなりのがんばりが必要です。"
    End If
  Next

End Sub


Function g2(t As Byte, a As Byte)

  Dim i As Integer

  g2 = 0
  If t = 0 Then
    For i = 1 To 5
      If g2 < Cells(6 + a, 1 + i) Then g2 = Cells(6 + a, 1 + i)
    Next
  End If
  If t = 1 Then
    For i = 1 To 40
      If g2 < Cells(6 + i, 1 + a) Then g2 = Cells(6 + i, 1 + a)
    Next
  End If

End Function


Sub f5()

  Dim i As Byte

  '各生徒の最高点の算出
  Dim max As Integer
  For i = 1 To 40
    Cells(6 + i, 9) = g2(0, i)
  Next

End Sub


Sub f6()

  Dim i As Byte

  '各生徒の最低点の算出
  Dim min As Integer
  For i = 1 To 40
    Cells(6 + i, 10) = g3(0, i)
  Next

End Sub


Function g3(t As Byte, a As Byte)

  Dim i As Integer

  g3 = 500
  If t = 0 Then
    For i = 1 To 5
      If g3 > Cells(6 + a, 1 + i) Then g3 = Cells(6 + a, 1 + i)
    Next
  End If
  If t = 1 Then
    For i = 1 To 40
      If g3 > Cells(6 + i, 1 + a) Then g3 = Cells(6 + i, 1 + a)
    Next
  End If

End Function


Sub f7()

  Dim i As Byte

  '各教科と合計・平均の最高点の算出
  For i = 1 To 7
    Cells(49, 1 + i) = g2(1, i)
  Next

End Sub



Sub f8()

  Dim i As Byte

  '各教科と合計・平均の最低点の算出
  For i = 1 To 7
    Cells(50, 1 + i) = g3(1, i)
  Next

End Sub


Funcitonプロシージャ成績一覧表マクロの改良例その3(Functionプロシージャが2つの場合)
Private Sub CommandButton1_Click()

  f1 '生徒の合計点・平均点の計算
  f2 '各教科等の合計点・平均点の計算
  f3 '各生徒の合否判定
  f4 '各生徒の講評
  f5 '各生徒の最高点の算出
  f6 '各生徒の最低点の算
  f7 '各教科と合計・平均の最高点の算出
  f8 '各教科と合計・平均の最低点の算出

End Sub


Function g1(t As Byte, a As Byte)

  Dim i As Integer

  g1 = 0
  If t = 0 Then
    For i = 1 To 5
      g1 = g1 + Cells(6 + a, 1 + i)
    Next
  End If
  If t = 1 Then
    For i = 1 To 40
      g1 = g1 + Cells(6 + i, 1 + a)
    Next
  End If


End Function


Sub f1()

  Dim i As Byte

  '生徒の合計点・平均点の計算
  For i = 1 To 40
    Cells(6 + i, 7) = g1(0, i)
    Cells(6 + i, 8) = g1(0, i) / 5
  Next

End Sub


Sub f2()

  Dim i As Byte

  '各教科等の合計点・平均点の計算
  For i = 1 To 7
    Cells(47, 1 + i) = g1(1, i)
    Cells(48, 1 + i) = g1(1, i) / 40
  Next

End Sub


Sub f3()

  Dim i As Byte

  '各生徒の合否判定
  For i = 1 To 40
    If Cells(6 + i, 7) >= 300 Then
      Cells(6 + i, 11) = "合格"
    End If
    If Cells(6 + i, 7) < 300 Then
      Cells(6 + i, 11) = "不合格"
    End If
  Next

End Sub


Sub f4()

  Dim i As Byte, j As Byte

  '各生徒の講評
  For i = 1 To 40
    If Cells(6 + i, 7) >= 350 Then
      Cells(6 + i, 12) = "あなたはかなり優秀です。"
    End If
    If Cells(6 + i, 7) < 350 And Cells(6 + i, 7) >= 300 Then
    Cells(6 + i, 12) = "おめでとう。"
    End If
    If Cells(6 + i, 7) < 300 And Cells(6 + i, 7) >= 200 Then
      Cells(6 + i, 12) = "合格まで後一歩です。"
    End If
    If Cells(6 + i, 7) < 200 Then
      Cells(6 + i, 12) = "かなりのがんばりが必要です。"
    End If
  Next

End Sub


Function g2(t1 As Byte, t2 As Byte, a As Byte)

  Dim i As Integer

  If t1 = 0 Then
    g2 = 0
    If t2 = 0 Then
      For i = 1 To 5
        If g2 < Cells(6 + a, 1 + i) Then g2 = Cells(6 + a, 1 + i)
      Next
    End If
    If t2 = 1 Then
      For i = 1 To 40
         If g2 < Cells(6 + i, 1 + a) Then g2 = Cells(6 + i, 1 + a)
      Next
    End If
  End If
  If t1 = 1 Then
    g2 = 500
    If t2 = 0 Then
      For i = 1 To 5
        If g2 > Cells(6 + a, 1 + i) Then g2 = Cells(6 + a, 1 + i)
      Next
    End If
      If t2 = 1 Then
        For i = 1 To 40
           If g2 > Cells(6 + i, 1 + a) Then g2 = Cells(6 + i, 1 + a)
        Next
      End If
    End If

End Function


Sub f5()

  Dim i As Byte

  '各生徒の最高点の算出
  Dim max As Integer
  For i = 1 To 40
    Cells(6 + i, 9) = g2(0, 0, i)
  Next

End Sub



Sub f6()

  Dim i As Byte

  '各生徒の最低点の算出
   Dim min As Integer
     For i = 1 To 40
     Cells(6 + i, 10) = g2(1, 0, i)
   Next

End Sub


Sub f7()

  Dim i As Byte

  '各教科と合計・平均の最高点の算出
  For i = 1 To 7
    Cells(49, 1 + i) = g2(0, 1, i)
  Next

End Sub



Sub f8()

  Dim i As Byte

  '各教科と合計・平均の最低点の算出
  For i = 1 To 7
   Cells(50, 1 + i) = g2(1, 1, i)
  Next

End Sub






第5話へ 第7話へ

004


vc++講義へ
vb講義へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座へ
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ

数学研究室に戻る