VBA简单代码

时间:2014-11-24 21:19:07

标签: excel-vba vba excel

Private Sub cmdplay_click()

    LblGameName.Caption = "Hello! And welcome to Unicorn Adventure Extream! Are you excited?"
    CmdExit.Caption = "no"
    CmdPlay.Caption = "yes"
    If CmdPlay = True Then
        LblGameName.Caption = "As you should be!! Welcome to Unicopitopia! Will you please enjoy your stay?"
    ElseIf CmdExit = True Then
        LblGameName.Caption = "You have angered the unicorn!! She ripped out your heart and devoured it! Bad Luck! Much Blood!"
    End If

End Sub

我不明白为什么当我按是或否时我的标签没有改变?

1 个答案:

答案 0 :(得分:1)

您的示例存在一些问题。

您需要命名按钮并使其中的文本保持不变。一个可能是cmdYes,另一个可能是cmdExit。然后点击与这些按钮关联的事件。要实现这一点,只需双击表单对象设计器模式中的按钮,它将调出代码并生成单击事件。

根据你早先的命名,你肯定有错误的名字。

我希望你的Lbl命名为txtMessage,并将其作为锁定的文本框。您可以设置文本框的背景颜色以匹配UserForm颜色。然后只需将其文本设置为使用您尝试实现的任何打印输出进行更新。

Private Sub UserForm_Activate()

    txtMessage.Text =  "Hello! And welcome to Unicorn Adventure Extreme! Are you excited?"

End Sub 

然后为您的按钮分别设置事件。

Private Sub cmdExit_click()

    Dim exitMessage As String

    exitMessage = "You have angered the unicorn!! She ripped out your heart and devoured it! Bad Luck! Much Blood!"
    txtMessage.Text = exitMessage

    MsgBox("Game Over, Goodbye") ' or whatever else you want to have happen.  
    Unload Me      'You could unload the form here.

End Sub

每个按钮的单独事件。

Private Sub cmdYes_click()

    Dim YesMessage As String

    YesMessage = "As you should be!! Welcome to Unicopitopia! Will you please enjoy your stay?"
    txtMessage.Text = YesMessage

    'Call some other subroutine that launches your game.
End Sub