Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的vba又停止工作了?_Vba_Powerpoint - Fatal编程技术网

我的vba又停止工作了?

我的vba又停止工作了?,vba,powerpoint,Vba,Powerpoint,这一次我制作了选项框,所以我把答案周围有语音标记的代码放进去,然后我试着用它,但它就是不起作用。我输入的代码是: Private Sub OptionButton1_Click() If OptionButton1.Value = "Stores all the components" Then MsgBox "That is correct. Well done!" SlideShowWindows(1).View.Next Else

这一次我制作了选项框,所以我把答案周围有语音标记的代码放进去,然后我试着用它,但它就是不起作用。我输入的代码是:

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

有人能帮帮我吗?

我想你在试这个

If OptionButton1.Caption = "Stores all the components" 
还是这个

If OptionButton1.Value = True Then

VBA中的optionButton的值为0(未选中)或-1(已选中)。如果您正在查找optionbutton所显示的内容,我相信您需要的是.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)

请注意,这是非常丑陋的(您可能希望创建一个易于使用的调用方法和设置正确答案的方法),但它应该给您一些东西继续下去。

也许更漂亮一点

每个选项按钮单击事件调用检查应答:

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

如果我没记错的话(假设
OptionButton1
是一个OptionButton实例),
Value
属性是一个布尔值,而不是字符串。我不明白你想说什么,请进一步解释,见Siddharth的答案。我认为他在正确的轨道上。我已经给了你检查标题的代码,但我认为你实际上想检查值是否为真……那么我需要做什么呢?我觉得它应该是你需要的第二个,因为一个选项按钮可以有多少标题;)我把它放进去了,但它仍然不起作用,就像当我点击我想要的答案时,它说的是错的。当我点击其他答案时,再试一次。什么都没有发生。我只是尝试了第二个代码,它在用户表单和幻灯片中都起作用:)