Dim counter As Integer
Dim TotalQ As Integer, ans As Integer
Dim QuestionRow() As Long
Dim wsQuestions As Worksheet
Dim UserAnswers() As Variant
Private Sub Answer1_Click()
Button.Enabled = True
End Sub
Private Sub Answer2_Click()
Button.Enabled = True
End Sub
Private Sub Answer3_Click()
Button.Enabled = True
End Sub
Private Sub Answer4_Click()
Button.Enabled = True
End Sub
Private Sub Button_Click()
Dim NextRow As Long
'get answer & clear control for next question
For ans = 1 To 4
With Me.Controls("Answer" & ans)
If .Value Then .Value = False: Exit For
End With
Next
'*****************************************************************************************
'********************************store responses to array*********************************
'username
UserAnswers(counter, 1) = Environ("USERNAME")
'questions
UserAnswers(counter, 2) = wsQuestions.Cells(QuestionRow(counter), 1).Text
'selected answers
UserAnswers(counter, 3) = wsQuestions.Cells(QuestionRow(counter), ans + 1).Text
'*****************************************************************************************
ansacc = CInt(Range("Questions!F" & QuestionRow(counter)).Text)
If (ansacc = ans) Then
status.Width = status.Width + 30
End If
'get next question
GetQuestion xlNext
If counter > TotalQ Then
Me.Hide
MsgBox ("Your score is " & TotalQ * status.Width / 30 & "%")
'answers to worksheet
With ThisWorkbook.Worksheets("Answers")
'get next row
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
'post array to range
.Cells(NextRow, 1).Resize(UBound(UserAnswers, 1), 3).Value = UserAnswers()
End With
Unload Me
End If
End Sub
Private Sub BackButton_Click()
GetQuestion xlPrevious
End Sub
Sub GetQuestion(ByVal Direction As XlSearchDirection)
Dim i As Integer
Dim EnableButton As Boolean
'counter value
counter = IIf(Direction = xlNext, counter + 1, counter - 1)
If counter <= TotalQ Then
Question.Caption = wsQuestions.Cells(QuestionRow(counter), 1).Text
For i = 1 To 4
With Me.Controls("Answer" & i)
'get question
.Caption = wsQuestions.Cells(QuestionRow(counter), i + 1).Text
'show previous value
.Value = CBool(.Caption = UserAnswers(counter, 3))
If .Value Then EnableButton = True
End With
Next i
'show progress count
Me.Caption = "Question " & counter & " of " & TotalQ
End If
With Me.Button
.Enabled = EnableButton
.Caption = IIf(counter = TotalQ, "Finish", "Next >")
End With
With Me.BackButton
.Enabled = CBool(counter > 1)
.Caption = "< Back"
End With
End Sub
Private Sub UserForm_Initialize()
'******************************************************************************************
'******************DO NOT RENAME THIS EVENT TO MATCH YOUR USERFORM NAME********************
'******************************************************************************************
Dim i As Integer
Dim cell As Range
Set wsQuestions = ThisWorkbook.Worksheets("Questions")
'get total questions asked
TotalQ = wsQuestions.Range("I2").Value
'size arrays
ReDim UserAnswers(1 To TotalQ, 1 To 3)
ReDim QuestionRow(1 To TotalQ)
With wsQuestions
'store selected question rows to array
For Each cell In .Range(.Range("H2"), .Range("H" & .Rows.Count).End(xlUp)).Cells
If UCase(cell.Value) = "A" Then i = i + 1: QuestionRow(i) = cell.Row
Next
End With
'get first question
GetQuestion xlNext
End Sub