我的vba又停止了工作?

时间:2013-10-10 17:09:38

标签: powerpoint powerpoint-vba

这次我正在制作选项框,所以我在代码中放入了带有语音标记的答案,然后我尝试使用它,但它只是不起作用。我输入的代码是:

Private Sub OptionButton1_Click()
    If OptionButton1.Value = "Stores all the components" Then
        MsgBox "That is correct. Well done!"
        SlideShowWindows(1).View.Next
    Else
        MsgBox "Sorry, that is not right. Try again"
    End If
End Sub

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

我想你正在尝试这个

If OptionButton1.Caption = "Stores all the components" 

或本

If OptionButton1.Value = True Then

答案 1 :(得分:0)

VBA中的optionButton的值为0(未选中)或-1(已选中)。如果你正在寻找选项按钮所说的内容,我相信你想要.Caption而不是.Value

编辑: 在再次查看您的代码后,我发现了一个根本性的缺陷。您将此选项分配给选项单击,因此如果第一个按钮没有正确的值,则不会发生任何事情。我认为你需要的是像这样的子程序

Private Sub checkAnswer (answer as string)
    dim expecTedAnswer as string
    expectedAnswer = "The correct answer"
    if answer = expectedanswer then
        msgbox "Correct"
    else
        msgbox "Incorrect"
    end if
End sub

然后在选项中单击,只需调用Sub

CheckAnswer(OptionButton1.Caption)

请注意,这非常难看(您可能希望创建一个易于使用的调用方法和设置correctAnswer的方法),但它应该可以让您继续。

答案 2 :(得分:0)

或许有点漂亮......

每个OptionButton点击事件都会调用CheckAnswer:

Private Sub OptionButton1_Click()
    If CheckAnswer(Me.OptionButton1, "Correct answer") Then
        MsgBox "Good user.  GOOD!"
    Else
        MsgBox "Bad user!  BAD!  Go stand in the corner."
    End If
End Sub

Private Sub OptionButton2_Click()
    If CheckAnswer(Me.OptionButton2, "Correct answer") Then
        MsgBox "Good user.  GOOD!"
    Else
        MsgBox "Bad user!  BAD!  Go stand in the corner."
    End If
End Sub

Function CheckAnswer(oCtl As Object, sCorrectAnswer As String) As Boolean
    If UCase(oCtl.Caption) = UCase(sCorrectAnswer) Then
        CheckAnswer = True
    Else
        CheckAnswer = False
    End If
End Function