第9講 社員が自分に命令することを繰り返す(プロシージャの再帰的使用)
第7話 社員が自分に命令を繰り返すことによる等差数列の積の計算

シート

プログラムの文章
Private Sub CommandButton1_Click()
  Dim a As Integer, b As Integer, c As Integer
  a = Cells(5, 2)
  b = Cells(5, 4)
  c = Cells(5, 6)
  Cells(6, 1) = "等差数列の積"
  Cells(7, 1) = f(a, b, c)
End Sub
Private Sub CommandButton2_Click()
  Rows("6:200").Select
  Selection.ClearContents
  Cells(5, 2).Select
  Selection.ClearContents
  Cells(5, 4).Select
  Selection.ClearContents
  Cells(5, 6).Select
  Selection.ClearContents
  Cells(1, 1).Select
End Sub
Function f(a As Integer, b As Integer, c As Integer)
  If b - c >= a Then f = b * f(a, b - c, c) Else f = a
End Function
実行結果例


次の課題です。
マクロ
vb
Private Sub CommandButton1_Click()
  Dim a As Integer, b As Integer, c As Integer
  a = Cells(5, 2)
  Cells(6, 1) = f(a)
End Sub
Private Sub CommandButton2_Click()
  Rows("6:200").Select
  Selection.ClearContents
  Cells(5, 2).Select
  Selection.ClearContents
  Cells(1, 1).Select
End Sub
Function f(a As Integer)
  If a - 1 >= 0 Then f = a + f(a - 1) Else f = a
End Function
をSubプロシージャによって実現しましょう。

第6話へ 第8話へ

トップ

vc++講義へ

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