VBA简单代码
问题描述:
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
我不明白为什么我的标签没有改变,当我按是或否?VBA简单代码
答
你的例子有几个问题。
你会想让你的按钮命名并让它们中的文本保持不变。一个可能是cmdYes,另一个是cmdExit。然后点击与这些按钮关联的点击事件。为了实现这一点,只需双击表单对象设计器模式中的按钮,它将调出代码并生成一个单击事件。
根据你以前的命名,你肯定有东西被误称。
我想你已经命名了类似txtMessage的Lbl,并将其作为锁定文本框。您可以设置文本框的背景颜色以匹配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
在你的cmdPlay子句中,你引用了“lblnamegame”。你的意思是“LblGameName”? – Tmdean 2014-11-24 21:21:13
哦..哎呀!是!谢谢!我已经搞乱了很多,它仍然没有工作后,我已经修复,虽然 – Sphyrna 2014-11-24 21:23:59
没有更多的细节很难解决问题。您应该描述表格的外观,您正在做什么,您期望发生什么以及实际发生的情况。否则,我们能做的最好的就是寻找错别字。 – Tmdean 2014-11-24 21:34:52