第12講 For文以外の様々なループ文
第4話 Do While文による様々な計算
01
0203
未入力の場合
04
をDo While文で実現するプログラム例
Private Sub CommandButton1_Click()
  
  Range("C6").Select
  Selection.ClearContents
  Dim m As Long, n As Long, h As Long
  If Cells(4, 2) = "" Or Cells(5, 2) = "" Or Cells(6, 2) = "" Then
    Cells(6, 3) = "入力欄をすべて埋めてから再び実行ボタンを押してください。"
    GoTo tobi
  End If
  m = Cells(4, 2)
  n = Cells(5, 2)
  h = Cells(6, 2)
  Cells(7, 6) = f1(m, n, h)
  Cells(8, 6) = f2(m, n, h)
  Cells(9, 6) = f3(m, n, h)
  Cells(10, 6) = f4(m, n, h)
tobi:
  Cells(1, 1).Select
  
End Sub
Function f1(m As Long, n As Long, h As Long)

  Dim w As Long, i As Long
  i = m
  Do While i <= n
    w = w + i
    i = i + h
  Loop
  f1 = w
  
End Function
Function f2(m As Long, n As Long, h As Long)

  Dim w As Long, i As Long
  i = m
  Do While i <= n
    w = w + i * i
    i = i + h
  Loop
  f2 = w
  
End Function
Function f3(m As Long, n As Long, h As Long)

  Dim w As Long, i As Long
  i = m
  Do While i <= n
    w = w + i * i * i
    i = i + h
  Loop
  f3 = w
  
End Function
Function f4(m As Long, n As Long, h As Long)

  Dim w As Long, i As Long
  i = m
  Do While i <= n
    w = w + i * i * i * i
    i = i + h
  Loop
  f4 = w
  
End Function
Private Sub CommandButton2_Click()
  
  Range("B4:B6,F7:F11,C6").Select
  Selection.ClearContents
  Range("A1").Select
  
End Sub

参考ダウンロード添付ファイル

今度は、同じ課題をDo...Loop While文で実現してみましょう。


第3話へ 第5話へ
004

eclipse c++ 入門
魔方陣 数独で学ぶ VBA 入門
数独のシンプルな解き方・簡単な解法の研究
vc++講義へ
excel 2013 2010 2007 vba入門へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座へ
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ
専門用語なしの C言語 C++ 入門(Visual C++ 2010で学ぶ C言語 C++ 入門)
専門用語なしの excel vba マクロ 入門 2013 2010 2007 対応講義 第1部
eclipse java 入門へ
excel 2016 vba 入門へ
小学生からエンジニアまでのRuby入門へ
本サイトトップへ