1 / 9

Dim i As Integer ' カウンタ用変数 Dim a As String ' 文字取得用変数

Dim i As Integer ' カウンタ用変数 Dim a As String ' 文字取得用変数 a = TextBox1.Text ' テキストボックスから文字を取得 For i = 1 To 10 'A 列、 B 列をクリア Cells(i, 1).Value = "" Cells(i, 2).Value = "" Next

Download Presentation

Dim i As Integer ' カウンタ用変数 Dim a As String ' 文字取得用変数

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dim i As Integer 'カウンタ用変数 Dim a As String '文字取得用変数 a = TextBox1.Text 'テキストボックスから文字を取得 For i = 1 To 10 'A列、B列をクリア Cells(i, 1).Value = "" Cells(i, 2).Value = "" Next If CheckBox1.Value = True Then '「A1~A10にコピー」にチェックが入っていたら For i = 1 To 10'1~10行 Cells(i, 1).Value = a'A列にaの内容を表示 Next End If If CheckBox2.Value = True Then '「A1~A10にコピー」にチェックが入っていたら For i = 1 To 10'1~10行     Cells(i, 2).Value = a'B列にaの内容を表示 Next End If

  2. ・アルゴリズム別室 For~Nextの使い方について・アルゴリズム別室 For~Nextの使い方について If CheckBox1.Value = True Then For i = 1 To 10 Cells(i, 1).Value = a Next End If If CheckBox2.Value = True Then For i = 1 To 10 Cells(i, 2).Value = a Next End If 同じ命令(For i=1 to 10~Next)が 繰り返されるので、一見無駄に見える 最低限の命令しか実行されないので、 実行速度は速い! 各処理がIf文内で完結している If ~ Then判定:2回 For ~ Nextループ:0 ~20回 2つのFor~Nextを一つにして、For~Nextループ中で If~Thenを用いて挙動を変化させてみる For i = 1 To 10 If CheckBox1.Value = True Then Cells(i, 1).Value = a End If If CheckBox2.Value = True Then Cells(i, 2).Value = a End If Next 全体で一つのブロックとなるため、すっきりするが、 プログラムがやや読みにくい。 ただし、If文のブロックが簡単になるため、 どのような条件で判断しているのかを比較しやすい 命令のステップ数が同じになるので、 実行速度は常に一定 If ~ Then判定:20回 For ~ Nextループ:常に10回

  3. Dim c As Integer 'カウンタ Dim v As Double, a As Double '平均算出用,どんな数字も大丈夫 Dim b As String c = 0 'カウンタのクリア v = 0 '計算用変数のクリア Do '連続データのチェック c = c + 1 b = Cells(c, 1).Value If b = "" Then Exit Do a = Cells(c, 1).Value 'セルの内容を変数aに取り込む v = v + a '和の算出 Loop If c > 1 Then 'c>1(二つ以上数字が入っていたら TextBox1.Text = v '平均の算出 TextBox2.Text = v / (c - 1) Else '数字が入っていなかったら TextBox1.Text = "計算不可" '「計算不可」と表示 TextBox2.Text = "計算不可" End If Dim c As Integer 'カウンタ c = 0 'テキストボックスの初期化 TextBox1.Text = "" TextBox2.Text = "" Do c = c + 1 'カウントアップ b = Cells(c, 1).Value 'c行のセルの内容チェック Cells(c, 1).Value = "" '強制的にセルを空白にする If b = "" Then Exit Do '連続データがなくなったら終了 Loop

  4. フォントいじり 料金計算 If CheckBox1.Value = True Then a = a + 360 '大人一人 If CheckBox2.Value = True Then a = a + 180 '子供一人 If CheckBox3.Value = True Then a = a + 720 '大人二人 If CheckBox4.Value = True Then a = a + 360 '子供二人 If a > 0 Then TextBox1.Text = a & "円です" For i = 1 To 10 Cells(i, 1).Font.Size = 10 + i Cells(i, 2).Font.Color = 255 Next チェックの状態で計算を変える チェックボックスをボタンで操作 c = a + b d = a * b If CheckBox1.Value = True Then TextBox1.Value = c End If If CheckBox2.Value = True Then TextBox2.Value = d End If If CheckBox1.Value = True Then CheckBox1.Value = False End If If CheckBox2.Value = True Then CheckBox2.Value = False End If テクニックは後述

  5. 世界のナベアツ的なプログラム For c = 1 To b aho = Int(c / 10) * 10 '1の位を0にする。 nabe = c - aho 'cをahoで引くことで、1の位だけを残す If c Mod 3 = 0 Or nabe = 3 Then '3の倍数または1の位が3の時 MsgBox "aho" Cells(c, 1).Value = "aho" Else Cells(c, 1).Value = c MsgBox c End If Next テクニックは後述 For i = 1 To 10 a = Int(Rnd(1) * 9 + 1) b = Int(Rnd(1) * 9 + 1) c = InputBox(a & "×" & b & "=", "第" & i & "問") If c = a * b Then MsgBox "正解" x = x + 1 Cells(i + 1, 4).Value = "○" Cells(i + 1, 4).Font.Color = -4165632 Else Cells(i + 1, 4).Value = "×" Cells(i + 1, 4).Font.Color = -16776961 End If Cells(i + 1, 1).Value = a & "×" & b & "=" Cells(i + 1, 2).Value = c Cells(i + 1, 3).Value = a * b Next 九九検定プログラム

  6. 数字の制御 - 1の位を0にする。 aho = Int(c / 10)* 10 例:c = 512の時 1 cを1/10にする c / 10 = 512 / 10 = 51.2 応用1の位を常に5にする aho = Int(c / 10)* 10+ 5 2 Int関数を使って小数を切り捨てる Int(c / 10) = Int(51.2) = 51 3 10倍して1の位を0にする。 Int(c / 10)* 10 = 51 * 10 = 510 論理演算子の使い方 論理演算子は、()の中が成立したらTrue(-1)、不成立ならFalse(0)になる。 例: a = 1 MsgBox (a = 1) チェックボックスのチェック状態を反転する CheckBox2.Value = (CheckBox2.Value = False) チェックボックス2にチェックが入っていない (CheckBox2.ValueがFalse)  →Trueに置き換わり、チェックが入る チェックボックス2にチェックが入っている (CheckBox2.ValueがTrue)  →Falseに置き換わり、チェックが外れる a = 1なので、Trueに置き換わり、 MsgBox (True)と同じになる。 a = 1 MsgBox (a = 1) * 10 a = 1かつ数字が入っているので、 数字扱いの -1に置き換わり、 MsgBox (-1)*10 と同じになる。

More Related