Skip to content

Instantly share code, notes, and snippets.

@cpsaku
Created May 3, 2018 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsaku/73cd1ca9973ff556913089df1e6a821c to your computer and use it in GitHub Desktop.
Save cpsaku/73cd1ca9973ff556913089df1e6a821c to your computer and use it in GitHub Desktop.
Option Explicit
Dim CorrectAns
Private Sub UserForm_Initialize()
info.Visible = False
End Sub
Private Sub ToggleButton5_Click()
setQuizData
Do
While info.Visible = False
DoEvents
Wend
Dim nextQuiz
nextQuiz = MsgBox("次の問題に進みますか?", vbInformation + vbYesNo)
If nextQuiz = vbYes Then
info.Visible = False
setQuizData
Else
End If
Loop
MsgBox "問題集を終了します", vbInformation + vbOKOnly
Unload Me
End Sub
Private Sub setQuizData()
Dim rowNo
rowNo = Int(Rnd * Sheet1.UsedRange.Rows.Count + 1)
quizText.Text = Sheet1.Cells(rowNo, 2)
ans1.Value = False
ans2.Value = False
ans3.Value = False
ans4.Value = False
ans1.Caption = ""
ans2.Caption = ""
ans3.Caption = ""
ans4.Caption = ""
'変数の説明
'ansFlag: いくつ選択肢を設定したのかを記憶しておく箱
'ansNo: 1から4の間で発生させた乱数の値を記憶しておく箱
'colNo: Sheet1の3列目から6列目に格納されている選択肢の、何番目までを設定したのかを記憶しておく箱
Dim ansFlag, ansNo, colNo
ansFlag = 0
ansNo = 0
colNo = 3
While ansFlag < 4 'ansFlagが4より小さいあいだ処理をくり返す
ansNo = Int(Rnd * 4 + 1) '0~1までの乱数Rnd に4をかけ、1を足し、小数点以下を切り捨てるInt
If UserForm1.Controls("ans" & ansNo).Caption = "" Then
UserForm1.Controls("ans" & ansNo).Caption = Sheet1.Cells(rowNo, colNo)
ansFlag = ansFlag + 1
'正答(Sheet1の3列目)がどのトグルボタンに設定されたかをCorrectAnsに記憶
If colNo = 3 Then
CorrectAns = ansNo
End If
End If
colNo = colNo + 1
Wend
CorrectAns = 1
End Sub
Private Sub answerJudg(tName)
If UserForm1.Controls("ans" & tName).Value = False Then
Exit Sub
End If
If CorrectAns = tName Then
info.Caption = "○ 正解"
Else
info.Caption = "× 不正解"
End If
info.Visible = True
End Sub
Private Sub ans1_Click()
answerJudg (1)
End Sub
Private Sub ans2_Click()
answerJudg (2)
End Sub
Private Sub ans3_Click()
answerJudg (3)
End Sub
Private Sub ans4_Click()
answerJudg (4)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment