VBA简单算术然后显示正确的数量

时间:2018-08-01 13:59:42

标签: vba excel-vba userform calculation

如何在简单的算术用户表单上获得正确答案。我有加法,减法,乘法和除法。用户在四个选项上选择,然后将生成一个随机的两个数字,然后用户可以输入他的答案,然后它将计算答案是正确还是错误。我的问题是如何获得用户的正确答案,将其除以5?

这是我的VBA代码用户表单。

Option Explicit
Dim num1 As Integer
Dim num2 As Integer
Dim output As Integer
Dim i As Integer
Dim g As Double
Dim t As Integer

//ADDITION
Private Sub CommandButton1_Click()

Label2.Caption = ""
TextBox1.Value = ""
num1 = Rnd * 10 + 1
num2 = Rnd * 10 + 1
output = num1 + num2
Label1.Caption = "What is " + Str(num1) + " + " + Str(num2) + "?"
End Sub

//SUBTRACTION
Private Sub CommandButton2_Click()

Label2.Caption = ""
TextBox1.Value = ""
num1 = Rnd * 10 + 1
num2 = Rnd * 10 + 1
output = num1 - num2
Label1.Caption = "What is " + Str(num1) + " - " + Str(num2) + "?"
End Sub

//MULTIPLICATION
Private Sub CommandButton3_Click()

Label2.Caption = ""
TextBox1.Value = ""
num1 = Rnd * 10 + 1
num2 = Rnd * 10 + 1
output = num1 * num2
Label1.Caption = "What is " + Str(num1) + " * " + Str(num2) + "?"
End Sub

//DIVISION
Private Sub CommandButton4_Click()

Label2.Caption = ""
TextBox1.Value = ""
num1 = Rnd * 10 + 1
num2 = Rnd * 10 + 1
output = num1 / num2
Label1.Caption = "What is " + Str(num1) + " / " + Str(num2) + "?"
End Sub

//ENTER BUTTON
Private Sub CommandButton5_Click()

If output = TextBox1.Value Then
    Label2.Caption = "Correct!"
    TextBox1.Value = ""
Else
    Label2.Caption = "Wrong " + Str(output) + " is the correct answer"
    TextBox1.Value = ""
End If
End Sub

//GENERATE GRADE RESULT BUTTON
Private Sub CommandButton7_Click()

If Label2.Caption = "Correct" Then
t = 1

g = t / 5 * 100

MsgBox (" You got " + Str(t) + " correct answers out of 5 questions for " + Str(g) + "%")

End If
End Sub

这是我的用户表单的屏幕截图 enter image description here

只需使“成绩”按钮起作用即可。 非常感谢您的帮助谢谢。

0 个答案:

没有答案