Excelで基礎講座ソフトを作ってみよう

Step 7 問題3(計算式)の作成

問題3は「足し算」を入力する練習問題です。


Homeに戻る > Excelでアプリケーションソフト > 基礎講座ソフトを作ってみよう

シートのVBAコード

下記のVBAコードを追加してください。

'問題3 答えのチェック
Private Function Ex3AnswerCheck() As Boolean
    Dim s1 As String
    Dim ln As Long
    Ex3AnswerCheck = False
On Error GoTo ErrExit:
    '数値のチェック
    ln = Range("B10") + 56
    If ln = Range("B11") Then
        '計算式を取得
        s1 = Range("B11").Formula
        '「$」を取り除く
        ExReplaceStr s1, "$", ""
        '「B10」と「56] と 「+」があれば正解とする
        If InStr(s1, "B10") > 0 And InStr(s1, "56") > 0 And InStr(s1, "+") > 0 Then
            Ex3AnswerCheck = True
        End If
    End If
    
    Exit Function
ErrExit:
End Function

標準モジュールに下記のVBAコードを追加してください。

'文字列の置き換え
'src: 元の文字
'fnd: 置き換え前の文字
'repl: 置き換え後の文字
Public Sub ExReplaceStr(src As String, fnd As String, repl As String)
    Dim delpos As Long
    Dim startpos As Long
    Dim before As String
    Dim after As String
    
    '検索開始位置
    startpos = 1
    '開始の検索結果位置
    delpos = InStr(startpos, src, fnd)

    Do Until delpos = 0
        '検索位置から前の文字
        before = Left(src, delpos - 1)
        '検索位置から後の文字
        after = Right(src, (Len(src) - (delpos + Len(fnd) - 1)))
        '置き換え後の文字
        src = before & repl & after
        '次の位置から繰り返す
        startpos = Len(before) + Len(repl) + 1
        delpos = InStr(startpos, src, fnd)
    Loop
End Sub

下記のVBAコードに変更してください。

'出来たボタン
Private Sub CommandButton2_Click()
    Dim bret As Boolean
    
    '終了フラッグ
    endFlag = True
    DoEvents
    
    Select Case ComboBox1.ListIndex
        Case 0: bret = Ex1AnswerCheck   '問題1
        Case 1: bret = Ex2AnswerCheck   '問題2
        Case 2: bret = Ex3AnswerCheck   '問題3
    End Select
    
    If bret Then
        Label6.ForeColor = &H8000&
        Label6.Caption = "正解です。よくできました。"
        '次の問題を表示する
        If ComboBox1.ListCount - 1 > ComboBox1.ListIndex Then
            CommandButton3.Caption = "次の問題"
        Else
            '問題は終了
            MsgBox "正解です。よくできました。" & vbCrLf & "問題はこれで終了です。お疲れ様でした。"
            ComboBox1.ListIndex = 0
            Label4.Caption = ComboBox1.Column(1)
            Label2.Caption = ""
            Label6.Caption = ""
            CommandButton3.Caption = "始める"
        End If
    Else
        CommandButton3.Caption = "やり直す"
        Label6.ForeColor = vbRed
        Label6.Caption = "間違っています。"
    End If
    
    '経過時間をリセット
    Label1.Caption = 0
    Label3.Caption = "開始ステップを選択し、「始める」ボタンをクリックしてください。"
    ComboBox1.Enabled = True
    CommandButton1.Enabled = False
    CommandButton2.Enabled = False
    CommandButton3.Enabled = True
End Sub

'開始ボタン
Private Sub CommandButton3_Click()
    If CommandButton3.Caption <> "始める" And Label6.Caption <> "間違っています。" Then
        If ComboBox1.ListCount - 1 > ComboBox1.ListIndex Then
            ComboBox1.ListIndex = ComboBox1.ListIndex + 1
        Else
            '問題は終了
            ComboBox1.ListIndex = 0
            MsgBox "問題はこれで終了です。お疲れ様でした。"
            CommandButton3.Caption = "始める"
        End If
        Label4.Caption = ComboBox1.Column(1)
    End If
    
    Label6.Caption = ""
    CommandButton1.Enabled = True
    CommandButton2.Enabled = True
    CommandButton3.Enabled = False
    ComboBox1.Enabled = False
    
    '終了フラッグをリセット
    endFlag = False
    Label3.Caption = "ワークシート上で、課題を実行してください。"
    
    Select Case ComboBox1.ListIndex
        Case 0: '問題1
            Label2.Caption = "[ B10 ] セルに [ 125 ] と入力してください。"
            ExDoexercise (20)
        Case 1: '問題2
            Label2.Caption = "[ D3 ] セルに [ 地球にやさしい ] と入力してください。"
            ExDoexercise (10)
        Case 2: '問題3
            Range("B10") = 125
            Label2.Caption = "[ B11 ] セルに セル[ B10 ]に56をプラスする計算式を入力してください。"
            ExDoexercise (15)
    End Select
    
    Label3.Caption = "開始ステップを選択し、「始める」ボタンをクリックしてください。"
    ComboBox1.Enabled = True
    CommandButton1.Enabled = False
    CommandButton2.Enabled = False
    CommandButton3.Enabled = True
End Sub



実行中の画面

■問題3 [ B11 ] セルに セル[ B10 ]に56をプラスする計算式を入力してください。


■正解画面 B11には 「=B10+56」が入力されています。


Homeに戻る > Excelでアプリケーションソフト > 基礎講座ソフトを作ってみよう

■■■
このサイトの内容を利用して発生した、いかなる問題にも一切責任は負いませんのでご了承下さい
■■■
当ホームページに掲載されているあらゆる内容の無許可転載・転用を禁止します


Copyright (c) Excel-Excel ! All rights reserved